nginx.config

70 阅读1分钟

#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       8080;
    server_name  localhost;

    # 静态资源根目录
    root /usr/share/nginx/html/dist;

    # 默认索引页
    index index.html index.htm;

    # 静态资源处理
    location / {
        root   html/dist;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    # 处理API请求
    # 匹配所有以 /prod-api/ 开头的请求路径
    location /prod-api/ {
        # 将原始请求的 Host 头(如域名)传递给后端
        proxy_set_header Host $host;
        # 将客户端的真实 IP 传递给后端(用于日志或业务逻辑)
        proxy_set_header X-Real-IP $remote_addr;
        # 追加客户端 IP 到 X-Forwarded-For 头(用于追踪请求链)
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 匹配的请求转发到本机的 8886 端口服务
        # **注意**:`proxy_pass` 末尾的 `/` 会移除匹配到的 `/prod-api/` 路径前缀。
        proxy_pass http://localhost:8090/;
    }
}

        # 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;
    #    }
    #}