本文最后更新于 2024年7月5日 下午
安装 1 2 3 4 5 sudo apt-get update sudo apt-get install nginx
1 2 3 4 5 6 7 8 9 sudo systemctl status nginx sudo systemctl start nginx sudo systemctl stop nginx sudo systemctl restart nginx
📢:对nginx配置文件修改之后,都要重启nginx服务,加载修改后的配置文件
查看文件结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ubuntu@VM-16-2-ubuntu:~$ 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 6 directories, 18 files
配置文件内容
nginx.conf (为了方便看,我删掉了初始内容中所有带注释的代码)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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; ssl_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/*; }
要注意的是,在/var/www/html
目录中,文件的名字不是index.html
,原名为index.nginx.debian.html
,改成前者即可。
通过三处修改,完成从sites-enable
到conf.d
的迁移
在nginx.conf
中注释掉include /etc/nginx/sites-enabled/*
;
在conf.d
目录下新建static.conf
,添加如上文件内容
修改/var/www/html
目录中的文件名为index.html
1 2 3 4 5 6 7 nginx -t sudo systemctl restart nginx
配置静态服务器
将文件上传到/var/www/html
,如果有多个文件,可以新建多个文件,并在 nginx.conf
中配置,并重启服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 server { listen 80; server_name localhost; charset utf-8; location / { root /var/www/html; index index.html index.htm; } location /markdown { alias /var/www/html/markdown; index index.html index.htm; } }
配置端口转发
配置服务器上的端口转发,需要在服务器安全组开启对应端口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 ; location /eat/ { proxy_pass http://localhost:8822/; } }
配置域名
将购买好的域名配置到服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 ; location /eat/ { proxy_pass http://localhost:8822/; } }
配置https
为网站配置https
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 server { listen 80; server_name demo.southyang.cn; 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; location /eat/ { proxy_pass http://localhost:8822/; proxy_redirect off; } }