blogSite

我是谁?我在哪儿?我在干什么?

View project on GitHub

nginx

windows下常用命令

  • 启动 C:\Program Files\nginx-1.15.12>start nginx
  • 快速停止 C:\Program Files\nginx-1.15.12>nginx.exe -s stop
  • 完整停止 C:\Program Files\nginx-1.15.12>nginx.exe -s quit
  • 重新加载nginx.conf文件 C:\Program Files\nginx-1.15.12>nginx.exe -s reload
  • 查看nginx版本 C:\Program Files\nginx-1.15.12>nginx -v
  • linux下常用命令
  • ./nginx –启动
  • ./nginx -s stop –停止
  • ./nginx -s quit
  • ./nginx -s reload

反代理参考配置

    server {
        
        #监听端口
        listen 80;
        
        #服务器名称,也就是客户端访问的域名地址
        server_name a.xxx.com;
        
        #nginx日志输出文件
        access_log logs/nginx.access.log main;
        
        #nginx错误日志输出文件
        error_log logs/nginx.error.log;
        
        root html;
        
        index index.html index.htm index.php;
        
        location / {
            
            #被代理服务器的地址
            proxy_pass http://localhost:8081;
            
            #对发送给客户端的URL进行修改的操作
            proxy_redirect off;
            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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
            proxy_max_temp_file_size 0;
        }
        
    }
  • 根据需要选择性保留关键配置
    1. server_name:代表客户端向服务器发起请求时输入的域名;
    2. proxy_pass:代表源服务器的访问地址,也就是真正处理请求的服务器(localhost+端口号+[项目名]);
  • ps:去掉项目名称,增加proxy_cookie_path配置
      server {
            
          #监听端口
          listen 8081;
            
          #服务器名称,也就是客户端访问的域名地址
          server_name a.xxx.com;
            
          #nginx日志输出文件
          access_log logs/nginx.access.log main;
            
          #nginx错误日志输出文件
          error_log logs/nginx.error.log;
            
          root html;
            
          index index.html index.htm index.php;
            
          location / {
                
              #被代理服务器的地址
              proxy_pass http://localhost:8081/testProject/;
    
              proxy_cookie_path /testProject/ /;
                
              #对发送给客户端的URL进行修改的操作
              proxy_redirect off;
              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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
              proxy_max_temp_file_size 0;
          }
            
      }
    

PS

  • 如果是阿里云上部署,需要把监听的端口在安全组中进行配置
  • nginx 配置中的变量设置
      http {
          # nas ip
          map string $nasip {
                  default  "101.40.21.39";
          }
    
          # nas jenkins
          server {
                  listen 7070;
                  server_name 阿里云外网ip;
                  server_tokens off;
    
                  location / {
                          proxy_pass http://$nasip:7070;
                  }
          }
      }
    
  • 设置开机自启动
    1. 修改# vi /etc/rc.local文件,在末尾添加/usr/local/nginx/sbin/nginx/usr/local/tomcat/apache-tomcat-8.5.41/bin/startup.sh,nginx和tomcat的启动项所在位置
    2. 设置权限# chmod 755 /etc/rc.local
    3. 重启服务器查看效果

常见问题

  • 【部署问题】ginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)
    • 解决方法:

      /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
      

      使用nginx -c的参数指定nginx.conf文件的位置

配置参考

首页 > 学习总览 > 代理