nginx-config 常用配置,避坑记录
本文最后更新于:2021年9月11日 凌晨
Nginx常用配置,避坑记录
安装 Nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sudo yum update -y
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install -y nginx
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
|
配置 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| vim /etc/nginx/conf.d/文件名.conf
添加配置1(配置1和2可以同事监听80端口,绑定不同域名,实现一台服务器nginx同时配置多个前端网站,多个域名空格隔开) server { listen 80; server_name 域名1 域名2 localhost;
location / { proxy_set_header HOST $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:端口/; } }
添加配置2 server { listen 80; server_name 域名1 域名2 localhost;
location / { proxy_set_header HOST $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:端口/; }
}
代理静态网站 root指定磁盘目录 server { listen 80; server_name 域名1 域名2 多个域名空格隔开 localhost; location / { try_files $uri $uri/ /index.html; root D:\test-web; } }
|
常用命令
1 2 3 4 5 6 7 8
| 检查配置是否有误 sudo nginx -t
重载/重启 Nginx 配置 sudo nginx -s reload
查看nginx安装目录 ps -ef | grep nginx
|
错误及解决办法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 不能访问子路径错误: location /flow { proxy_pass http://127.0.0.1:9083/; } 修改: location /flow/ { proxy_pass http://127.0.0.1:9083/; } 解决:/flow 后面加个/
配置多个前端代码/a/ /b/不能访问 解决:把/a/ /b/放在根目录下面 (就是只有一个根目录,不过根目录里面可以放很多项目文件夹)
去掉请求后面的斜杠 添加在location节点里面 解决浏览器请求拼接到接口前面的诡异问题 rewrite ^/(.*)/$ /$1 permanent;
|
前后端分离项目部署
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
| location /gov_flow { alias /var/html/gov_flow; try_files $uri $uri/ /index.html; index index.html; }
location /stage-prod-api/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://flow:9083/; } location /stage-test-api/{ proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080/; }
server { listen 9991; server_name localhost; location /yqt/{ proxy_pass http://127.0.0.1:8888/yqt/; }
location /ad/{ proxy_pass http://127.0.0.1:8090/ad/; } }
|
nginx配置ssl证书示例1
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
| http{
server{ listen 443; server_name www.ilovey.live; ssl on; ssl_certificate /www/server/nginx/conf.d/4467149_www.ilovey.live.pem; ssl_certificate_key /www/server/nginx/conf.d/4467149_www.ilovey.live.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location ^~ /zhihao/ { proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080/; } }
server{ listen 80; server_name www.ilovey.live; rewrite ^/(.*)$ https://www.ilovey.live:443/$1 permanent; } }
|
nginx配置ssl证书示例2
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| server { listen 80; listen 443 ssl http2; server_name ilovey.live; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/网站目录;
ssl_certificate /www/server/nginx/conf.d/4467149_www.ilovey.live.pem; ssl_certificate_key /www/server/nginx/conf.d/4467149_www.ilovey.live.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; error_page 497 https://$host$request_uri;
location / { proxy_pass http://localhost:8886; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr;
}
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; }
location ~ \.well-known{ allow all; } }
|