Nginx
Nginx 常用配置 - 图1


安装 Nginx

  1. # 更新软件包,请确保服务器的软件包已经是最新的。
  2. sudo yum update -y
  3. # 添加 Nginx 源
  4. sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  5. # 安装 Nginx
  6. sudo yum install -y nginx
  7. # 启动 Nginx
  8. sudo systemctl start nginx.service
  9. # 设置开机自启 Nginx
  10. sudo systemctl enable nginx.service

配置 Nginx

  1. # 使用 vim 编辑创建 文件名.conf
  2. vim /etc/nginx/conf.d/文件名.conf

添加配置1(配置1和2可以同事监听80端口,绑定不同域名,实现一台服务器nginx同时配置多个前端网站,多个域名空格隔开)

  1. server {
  2. listen 80;
  3. server_name 域名1 域名2 localhost;
  4. location / {
  5. proxy_set_header HOST $host;
  6. proxy_set_header X-Forwarded-Proto $scheme;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. proxy_pass http://127.0.0.1:端口/;
  10. }
  11. }

添加配置2

  1. server {
  2. listen 80;
  3. server_name 域名1 域名2 localhost;
  4. location / {
  5. proxy_set_header HOST $host;
  6. proxy_set_header X-Forwarded-Proto $scheme;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. proxy_pass http://127.0.0.1:端口/;
  10. }
  11. }

代理静态网站 root指定磁盘目录

  1. server {
  2. listen 80;
  3. server_name 域名1 域名2 多个域名空格隔开 localhost;
  4. location / {
  5. try_files $uri $uri/ /index.html;
  6. root D:\test-web;
  7. }
  8. }

常用命令

检查配置是否有误

  1. sudo nginx -t

重载/重启 Nginx 配置

  1. sudo nginx -s reload

查看nginx安装目录

  1. ps -ef | grep nginx

错误及解决办法

不能访问子路径错误:

  1. location /flow {
  2. proxy_pass http://127.0.0.1:9083/;
  3. }

修改:

  1. location /flow/ {
  2. proxy_pass http://127.0.0.1:9083/;
  3. }

解决:/flow 后面加个/
配置多个前端代码/a/ /b/不能访问
解决:把/a/ /b/放在根目录下面 (就是只有一个根目录,不过根目录里面可以放很多项目文件夹)
去掉请求后面的斜杠 添加在location节点里面 解决浏览器请求拼接到接口前面的诡异问题

  1. rewrite ^/(.*)/$ /$1 permanent;

前后端分离项目部署

  1. ##前端配置
  2. location /gov_flow {
  3. ##放再nginx下的/content/gov_flow目录
  4. alias /var/html/gov_flow;
  5. try_files $uri $uri/ /index.html;
  6. index index.html;
  7. }
  8. ##后端接口配置
  9. location /stage-prod-api/ {
  10. proxy_set_header Host $http_host;
  11. proxy_set_header X-Real-IP $remote_addr;
  12. proxy_set_header REMOTE-HOST $remote_addr;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. proxy_pass http://flow:9083/;
  15. }
  16. location /stage-test-api/{
  17. proxy_set_header Host $http_host;
  18. proxy_set_header X-Real-IP $remote_addr;
  19. proxy_set_header REMOTE-HOST $remote_addr;
  20. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  21. proxy_pass http://localhost:8080/;
  22. }
  23. #两个后端项目接口合并成一个 给前端访问9991
  24. server {
  25. listen 9991;
  26. server_name localhost;
  27. location /yqt/{
  28. proxy_pass http://127.0.0.1:8888/yqt/;
  29. }
  30. location /ad/{
  31. proxy_pass http://127.0.0.1:8090/ad/;
  32. }
  33. }

nginx配置ssl证书示例1

  1. http{
  2. #http节点中可以添加多个server节点
  3. server{
  4. #ssl 需要监听443端口
  5. listen 443;
  6. # CA证书对应的域名
  7. server_name www.ilovey.live;
  8. # 开启ssl
  9. ssl on;
  10. # 服务器证书绝对路径
  11. ssl_certificate /www/server/nginx/conf.d/4467149_www.ilovey.live.pem;
  12. # 服务器端证书key绝对路径
  13. ssl_certificate_key /www/server/nginx/conf.d/4467149_www.ilovey.live.key;
  14. # session超时
  15. ssl_session_timeout 5m;
  16. # 协议类型
  17. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  18. # ssl算法列表
  19. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  20. # 是否 服务器决定使用哪种算法 on/off TLSv1.1 的话需要开启
  21. ssl_prefer_server_ciphers on;
  22. location ^~ /zhihao/ {
  23. proxy_set_header Host $host;
  24. proxy_pass http://127.0.0.1:8080/;
  25. }
  26. }
  27. # 如果用户通过 http 访问 直接重写 跳转到 https 这个是一个很有必要的操作
  28. server{
  29. listen 80;
  30. server_name www.ilovey.live;
  31. rewrite ^/(.*)$ https://www.ilovey.live:443/$1 permanent;
  32. }
  33. }

nginx配置ssl证书示例2

  1. server
  2. {
  3. listen 80;
  4. listen 443 ssl http2;
  5. server_name ilovey.live;
  6. index index.php index.html index.htm default.php default.htm default.html;
  7. root /www/wwwroot/网站目录;
  8. #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
  9. #error_page 404/404.html;
  10. ssl_certificate /www/server/nginx/conf.d/4467149_www.ilovey.live.pem;
  11. ssl_certificate_key /www/server/nginx/conf.d/4467149_www.ilovey.live.key;
  12. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  13. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  14. ssl_prefer_server_ciphers on;
  15. ssl_session_cache shared:SSL:10m;
  16. ssl_session_timeout 10m;
  17. error_page 497 https://$host$request_uri;
  18. #SSL-END
  19. #ERROR-PAGE-START 错误页配置,可以注释、删除或修改
  20. #error_page 404 /404.html;
  21. #error_page 502 /502.html;
  22. #ERROR-PAGE-END
  23. #PHP-INFO-START PHP引用配置,可以注释或修改
  24. #PROXY-START
  25. location /
  26. {
  27. proxy_pass http://localhost:8886;
  28. proxy_set_header Host $host;
  29. proxy_set_header X-Real-IP $remote_addr;
  30. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  31. proxy_set_header REMOTE-HOST $remote_addr;
  32. #持久化连接相关配置
  33. #proxy_connect_timeout 30s;
  34. #proxy_read_timeout 86400s;
  35. #proxy_send_timeout 30s;
  36. #proxy_http_version 1.1;
  37. #proxy_set_header Upgrade $http_upgrade;
  38. #proxy_set_header Connection "upgrade";
  39. ##expires 12h;
  40. }
  41. #PHP-INFO-END
  42. #禁止访问的文件或目录
  43. location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
  44. {
  45. return 404;
  46. }
  47. #一键申请SSL证书验证目录相关设置
  48. location ~ \.well-known{
  49. allow all;
  50. }
  51. }