广告
返回顶部
首页 > 资讯 > 服务器 >Ubuntu20.04安装配置Nginx
  • 559
分享到

Ubuntu20.04安装配置Nginx

nginx服务器ubuntu 2023-08-20 16:08:15 559人浏览 泡泡鱼
摘要

由于在学习配置时,网上的教程比较杂乱,用时很久才做好一些基础配置,把流程记录一下方便和我一样的小白学习 本文写于2023.2.10,如果间隔太久,下述内容可能会失效,请另寻教程 仅包含基础教程,个人服务未涉及到负载均衡 安装Ngin

由于在学习配置时,网上的教程比较杂乱,用时很久才做好一些基础配置,把流程记录一下方便和我一样的小白学习

本文写于2023.2.10,如果间隔太久,下述内容可能会失效,请另寻教程

仅包含基础教程,个人服务未涉及到负载均衡

服务器: 阿里云ubuntu20.04

nginx版本: nginx/1.18.0 (Ubuntu)

1. 安装nginx

1.1 安装及常用命令

# 更新apt-get源sudo apt-get update# 安装sudo apt-get install nginx# 安装后将自动开启nginx服务,打开浏览器输入ip即可查看初始页面
# 查看安装版本nginx -v# 输出:nginx version: nginx/1.18.0 (Ubuntu)
# systemctl命令# 查看状态sudo systemctl status nginx# 启动sudo systemctl start nginx# 停止sudo systemctl stop nginx# 重启sudo systemctl restart nginx

注意:对nginx配置文件修改之后,都要重启nginx服务,加载修改后的配置文件

1.2 文件结构

# 查看文件结构tree /etc/nginx
/etc/nginx├── conf.d├── fastcgi.conf├── fastcgi_params├── koi-utf├── koi-win├── mime.types├── modules-available├── modules-enabled│   ├── 50-mod-http-image-filter.conf -> /usr/share/nginx/modules-available/mod-http-image-filter.conf│   ├── 50-mod-http-xslt-filter.conf -> /usr/share/nginx/modules-available/mod-http-xslt-filter.conf│   ├── 50-mod-mail.conf -> /usr/share/nginx/modules-available/mod-mail.conf│   └── 50-mod-stream.conf -> /usr/share/nginx/modules-available/mod-stream.conf├── nginx.conf├── proxy_params├── scgi_params├── sites-available│   └── default├── sites-enabled│   └── default -> /etc/nginx/sites-available/default├── snippets│   ├── fastcgi-PHP.conf│   └── snakeoil.conf├── uwsgi_params└── win-utf

1.3 配置文件内容

nginx.conf (为了方便看,我删掉了初始内容中所有带注释的代码)

user www-data;worker_processes auto;pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf;events {worker_connections 768;}Http {sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;include /etc/nginx/mime.types;default_type application/octet-stream;ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLEssl_prefer_server_ciphers on;access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;gzip on;include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;}

最关键的是下面两行引入,上面的代码含义目前我还没研究,用到再说

include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;

这两行的意思是:从conf.d中加载所有后缀为conf的文件,从sites-enabled中加载所有文件,均作为配置文件

sites-enabled文件我用不习惯,因此我注释掉了这行,使用conf.d做配置文件

在conf.d中添加static.conf

server {    listen       80;    server_name  localhost;    charset utf-8; # 防止中文显示出现乱码    #access_log  logs/host.access.log  main;    location / {        root   /var/www/html; # 你的静态资源路径        index  index.html index.htm;# 访问的文件为html, htm    }}

要注意的是,在/var/www/html目录中,文件的名字不是index.html,原名为index.nginx.debian.html,改成前者即可。

通过三处修改,完成从sites-enableconf.d的迁移

  • nginx.conf中注释掉include /etc/nginx/sites-enabled/*;
  • conf.d目录下新建static.conf,添加如上文件内容
  • 修改/var/www/html目录中的文件名为index.html
# 检查配置文件是否有误nginx -t# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok# nginx: configuration file /etc/nginx/nginx.conf test is successful# 重启服务sudo systemctl restart nginx

2. 配置静态服务器

自行寻找一个网页做测试,上传到/var/www/html

我上传了之前写过的一个markdown在线编辑器,是一个文件夹,文件夹名为markdown

开始修改static.conf文件

server {    listen       80;    server_name  localhost;    charset utf-8; # 防止中文显示出现乱码    # 根据自己需要配置日志文件,可以单独配置,也可以全部放在/var/log/nginx的日志中    #access_log  logs/host.access.log  main;    location / {        root   /var/www/html; # 你的静态资源路径        index  index.html index.htm;# 访问的文件为html, htm    }        location /markdown {        alias   /var/www/html/markdown; # 你的静态资源路径        index  index.html index.htm;# 访问的文件为html, htm    }        # 后续如果有其他配置,模仿markdown的配置添加即可    # location /example {    #     alias   /var/www/html/example; # 你的静态资源路径    #     index  index.html index.htm;# 访问的文件为html, htm    # }}

对于多个路径的配置: [1]

  • 使用root会将location后的markdown追加在路径的尾部,在访问时就会访问到/var/www/html/markdown/markdown

  • 使用alias则不会将location后的markdown追加在路径尾部,访问时就为正确路径/var/www/html/markdown

如果添加charset utf-8;后还存在乱码,强制刷新一下试试

输入IP/markdown查看配置结果

3. 配置端口转发

自行寻找一个服务做测试,开在非80端口即可,我开在了8822端口

注:要在服务器的安全组配置,打开对应端口

首先测试一下 IP:8822 能不能正常使用,可以使用说明服务成功启动在8822端口,进行后续配置。

server {    listen       80;    server_name  localhost;     charset utf-8; # 防止中文显示出现乱码# 添加头部信息    proxy_set_header  Cookie $http_cookie;    proxy_set_header  X-Forwarded-Host $Host;    proxy_set_header  proxy_set_Server  $Host;    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;    # 访问IP/eat,则会自动访问对应地址IP:8822    location /eat/ {        proxy_pass http://localhost:8822/;    }        # 后续如果有其他配置,模仿eat的配置添加即可    # 访问IP/example,则会自动访问对应地址IP:port    # location /example/ {    #     proxy_pass http://localhost:port/;    # }}

输入IP/eat查看配置结果

4. 配置域名

我从阿里云购买了域名southyang.cn,将子域名demo.southyang.cn解析到服务器上

以端口转发为例:

server {    listen       80;    server_name  demo.southyang.cn;     charset utf-8; # 防止中文显示出现乱码# 添加头部信息    proxy_set_header  Cookie $http_cookie;    proxy_set_header  X-Forwarded-Host $Host;    proxy_set_header  proxy_set_Server  $Host;    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;    # 访问demo.southyang.cn/eat,则会自动访问对应地址IP:8822    location /eat/ {        proxy_pass http://localhost:8822/;    }        # 后续如果有其他配置,模仿eat的配置添加即可    # 访问IP/example,则会自动访问对应地址IP:port    # location /example/ {    #     proxy_pass http://localhost:port/;    # }}

输入demo.southyang.cn/eat查看配置结果

5. 配置https

以端口转发为例: [2]

server {    listen 80;    server_name demo.southyang.cn;    # 跳转https    return 301 https://$host$request_uri;}server {    listen 443   ssl http2;    server_name  demo.southyang.cn;     charset utf-8; # 防止中文显示出现乱码# 添加头部信息    proxy_set_header  Cookie $http_cookie;    proxy_set_header  X-Forwarded-Host $Host;    proxy_set_header  proxy_set_Server  $Host;    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;        # 配置证书    ssl_certificate 证书密钥地址    ssl_certificate_key 证书公钥地址    ssl_verify_client off;    proxy_ssl_verify off;    # 访问demo.southyang.cn/eat,则会自动访问对应地址IP:8822    location /eat/ {        proxy_pass http://localhost:8822/;        proxy_redirect off;    }        # 后续如果有其他配置,模仿eat的配置添加即可    # 访问IP/example,则会自动访问对应地址IP:port    # location /example/ {    #     proxy_pass http://localhost:port/;    # }}

输入demo.southyang.cn/eat查看配置结果

注: 目前我只用到了上述所列的内容,其他内容用到了再学

引用内容:

[1] https://blog.csdn.net/qq_39827677/article/details/113745095

[2] https://GitHub.com/Mereithhh/van-nav

来源地址:https://blog.csdn.net/qq_45951779/article/details/128969250

--结束END--

本文标题: Ubuntu20.04安装配置Nginx

本文链接: https://www.lsjlt.com/news/376525.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Ubuntu20.04安装配置Nginx
    由于在学习配置时,网上的教程比较杂乱,用时很久才做好一些基础配置,把流程记录一下方便和我一样的小白学习 本文写于2023.2.10,如果间隔太久,下述内容可能会失效,请另寻教程 仅包含基础教程,个人服务未涉及到负载均衡 安装ngin...
    99+
    2023-08-20
    nginx 服务器 ubuntu
  • Centos7安装配置nginx
    1.前言 在进行nginx安装前,考虑到本博客针对很多新手朋友我决定还是先介绍一些nginx知识,这样更加有利于各位读者朋友对nginx的理解,对后续学习也有很大帮助。 介绍 Nginx (engin...
    99+
    2023-08-31
    nginx 运维 centos linux 服务器
  • Ubuntu20.04安装配置GitLab的方法步骤
    介绍 GitLab CE或Community Edition是一个开源应用程序,主要用于托管Git存储库,以及其他与开发相关的功能,如问题跟踪。它旨在使用您自己的基础架构进行托管,并为您的开发团队提供部署内部存储库的灵活...
    99+
    2022-06-04
    Ubuntu20.04安装GitLab Ubuntu20.04 配置GitLab
  • Nginx安装配置详解
    不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦。不过...
    99+
    2022-11-13
  • nginx php-fpm安装配置
    nginx php-fpm安装配置 nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。 nginx一般是把请求发fas...
    99+
    2023-09-02
    nginx php 运维
  • Nginx如何安装配置
    这篇文章主要介绍“Nginx如何安装配置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Nginx如何安装配置”文章能帮助大家解决问题。简介Nginx 的安装:# CentOSyum&nbs...
    99+
    2023-07-02
  • nginx怎么安装配置
    这篇文章主要讲解了“nginx怎么安装配置”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“nginx怎么安装配置”吧!一、服务器基础配 置远程链接服务器ssh 用户名@公网ip默认的...
    99+
    2023-06-29
  • Nginx安装与配置详解
    Nginx负载均衡集群 一、Nginx简介1、nginx介绍2、反向代理2.1 什么是代理服务器?2.2 为什么要使用代理服务器?2.3 反向代理 VS 正向代理 3、负载均衡3.1 什么是负载均衡? 二、Nginx...
    99+
    2023-08-21
    nginx 服务器 运维
  • 安装配置gunicorn和NGINX的
    博客写得差不多了,打算部署到云上因为速度的关系,不打算部署在AWS上,于是申请了阿里云,环境是上篇文章提到的CentOS 7 64位安装和配置好Python3和MySQL,又稍微了解点web服务器的知识后,打算按这里的方法进行部署,思路是...
    99+
    2023-01-31
    gunicorn NGINX
  • Nginx的安装配置介绍
    这篇文章主要介绍“Nginx的安装配置介绍”,在日常操作中,相信很多人在Nginx的安装配置介绍问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Nginx的安装配置介绍”的疑惑有所帮助!接下来,请跟着小编一起来...
    99+
    2023-06-04
  • CentOS怎么安装配置Nginx
    要在CentOS上安装和配置Nginx,可以按照以下步骤进行操作:1. 更新系统软件包:```sudo yum update```2...
    99+
    2023-08-24
    CentOS Nginx
  • MacOS下安装和配置Nginx
    一、安装brew /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" 按回车后,根据提示操作:输入镜像序号 --> 输...
    99+
    2023-08-24
    nginx macos git
  • Nginx的安装配置教程
    一、Nginx的下载与安装 Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好   1.下载 在Ngin...
    99+
    2023-09-03
    nginx 前端 服务器
  • Windows 10 安装配置WSL2(ubuntu20.04)教程 超详细
    关于WLS的介绍1.什么是WSL?2 双系统的方法比较3 WSL1与WSL2比较 方法一: 传统手动安装1.1 windows系统版本查看及更新1.2 启用适用于Linux的Windw...
    99+
    2023-09-05
    linux 运维 服务器
  • nginx+php-fpm的安装和配置
    环境         虚拟机:VMware 16.2.4         OS:centos 7.6         远程连接工具:Xshell 7         nginx版本: nginx-1.14.2         php版本:p...
    99+
    2023-09-12
    服务器 nginx php linux
  • Linux安装配置nginx+php搭建
    Linux安装配置nginx+php搭建 文章目录 Linux安装配置nginx+php搭建1.nginx源码包编译环境和安装相应的依赖1.1 安装编译环境1.2 安装pcre库、zlib库和...
    99+
    2023-09-25
    linux nginx 运维
  • nginx mysql php如何安装配置
    本文操作环境:centos7系统、php 7.2.25版、DELL G3电脑nginx mysql php如何安装配置?CentOS7 下nginx与PHP mysql的安装与配置:下载Nginx  首先安装的依赖包:    gcc aut...
    99+
    2017-05-13
    nginx mysql php
  • nginx mysql php怎么安装配置
    本篇内容介绍了“nginx mysql php怎么安装配置”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!nginx mysql php安装配...
    99+
    2023-06-25
  • 详解ubuntu20.04下CLion2020.1.3安装配置ROS过程说明
    一 下载安装激活CLion 按照网上给的教程就可以 二 配置ROS 1.配置CLion的启动方式 在主目录打开隐藏文件.bashrc,命令是:sudo gedit ~/.bashrc 将CLion的启动文件clion.s...
    99+
    2022-06-04
    CLion2020.1.3安装配置ROS ubuntu20.04下CLion2020.1.3安装
  • linux下安装nginx后怎么配置
    本篇内容主要讲解“linux下安装nginx后怎么配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“linux下安装nginx后怎么配置”吧! 一、nginx安装nginx最好是直接在...
    99+
    2023-06-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作