nginx 基础配置

197 阅读3分钟

下载

命令

  • 进入nginx.exe同级目录下
start nginx // 启动(双击exe也能启动)

nginx -s reload // 重启

nginx -s stop // 停止

nginx -t // 检测配置文件格式是否正确

配置文件

  • 路径:nginx\conf\nginx.conf
  • 重写路径
http {
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location /api/  {
            rewrite ^/api/(.*) /$1 break; # 路径替换 /api 替换为 ''(空)
            # rewrite ^/api/(.*) /node/$1 break; # 路径替换 /api/ 替换为 /node/
            proxy_pass http://172.16.16.24:8080;  # 反向代理后端服务器的域名
            proxy_set_header Host $host;  
            proxy_set_header X-Real-IP $remote_addr;  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        }
        location /api/  {
            proxy_pass http://172.16.16.24:8080;  # 带/api/
            proxy_set_header Host $host;  
            proxy_set_header X-Real-IP $remote_addr;  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        }
        location /api/  {
            proxy_pass http://172.16.16.24:8080/;  # 不带/api/
            proxy_set_header Host $host;  
            proxy_set_header X-Real-IP $remote_addr;  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        }
        location / {
            root   /html/dist;
            index  index.html index.htm;
        }
   }
}

完整配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8666;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
       location /api  {
            rewrite ^/api/(.*) /$1 break;
            proxy_pass http://172.16.16.24:8080;  # 反向代理后端服务器的域名
            proxy_set_header Host $host;  
            proxy_set_header X-Real-IP $remote_addr;  
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        }
        location / {
            root   /html/dist;
            index  index.html index.htm;
        }
        
        # spa单页面,history路由配置
        error_page  404              /index.html;
        location = /index.html {
            root   html/dist;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

开启rewrite 日志(反向代理不生效,排查)

  • 设置nginx.conf配置文件中的错误日志级别为notice(nginx中最低级别的错误)并开启rewrite重写代理地址日志
error_log  logs/error.log  notice; # 开启低级别log

rewrite_log on; # 开启重写代理地址日志

demo

# user  nobody;
# error_log  logs/error.log;
error_log  logs/error.log  notice; # 开启低级别log
# error_log  logs/error.log  info;

http {
    rewrite_log on; # 开启重写代理地址日志
     # ... 其他配置 ...
}

log demo

image.png

完整log文本

  • logs/error.log
2024/03/29 09:54:42 [notice] 10044#32144: *30 "^/(.*)" matches "/demo/list", client: 127.0.0.1, server: localhost, request: "GET /demo/list HTTP/1.1", host: "localhost:6081", referrer: "http://localhost:6081/project/list"
2024/03/29 09:54:42 [notice] 10044#32144: *30 rewritten data: "/api/demo/list", args: "", client: 127.0.0.1, server: localhost, request: "GET /demo/list HTTP/1.1", host: "localhost:6081", referrer: "http://localhost:6081/project/list"
2024/03/29 09:55:03 [error] 10044#32144: *30 connect() failed (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /demo/list HTTP/1.1", upstream: "http://172.16.97.128:8098/api/demo/list", host: "localhost:6081", referrer: "http://localhost:6081/project/list"
2024/03/29 09:55:03 [error] 10044#32144: *30 CreateFile() "C:\nginx-1.24.0/html/dist1/50.html" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /demo/list HTTP/1.1", upstream: "http://172.16.97.128:8098/api/demo/list", host: "localhost:6081", referrer: "http://localhost:6081/project/list"