MALOUDA-PSA 2025-01-18 19:23 采纳率: 85.6%
浏览 6

nginx配置的问题


# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

worker_processes auto;

#pid C:\\Java\\nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
#include C:\sys\tools\nginx24\conf\*.conf;

events {
    worker_connections 1024;
}

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

    error_log C:\\sys\\tools\\nginx24\\logs\\nginxerror.log debug;
    access_log  C:\\sys\\tools\\nginx24\\logs\\access.log  main;


    
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             mime.types;
    default_type        text/html;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    #include C:\\sys\\tools\\nginx24\\conf\\*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         C:\\Java\\product\\html;

        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;

        location ^~/api {
            proxy_pass http://127.0.0.1:8022/api;
        }

    location ^~ /min/ {
        rewrite ^/min/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:8226/;
        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 X-Forwarded-Proto $scheme;

        access_log C:\\sys\\tools\\nginx24\\logs\\min_access.log;
         error_log C:\\sys\\tools\\nginx24\\logs\\min_error.log debug;
    }



        location ~ \.js$ {
            add_header Content-Type application/javascript;
        }

        error_page 404 /404.html;
        location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

配置后 使用本地访问 http://127.0.0.1/min 报错 404, 可是 http://127.0.0.1:8226 可以访问

  • 写回答

3条回答 默认 最新

  • 关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    看起来你的Nginx配置中存在一些问题,这可能是导致你访问 /min 路径时收到 404 错误的原因。以下是可能的问题及其解决方案:

    1. location 块中的重写规则: 在你的配置中,对于 /min/ 的 location 块,你使用了 rewrite 指令来重写 URL,但没有指定重写后的目标 URL。你需要明确指定重写后的目标 URL。例如,如果你想将 /min/ 重定向到 /,你可以使用以下配置:
    location ^~ /min/ {
        rewrite ^/min(.*)$ $1 break; # 这里使用 $1 来捕获 /min 之后的部分并重写它
        proxy_pass http://127.0.0.1:8226/;
        ...
    }
    

    注意,这里的 $1 用于捕获 /min/ 之后的所有内容并将其作为重写后的路径。如果你只想移除 /min/ 前缀并将请求转发到另一个端口上的后端服务器,这种重写方式应该是有效的。确保后端服务器处理这种类型的请求,并且能够正确地响应这些请求。

    1. 代理设置: 确保 proxy_pass 指令指向的后端服务器正在运行,并且正确处理请求。如果后端服务器无法处理来自 Nginx 的请求(例如由于错误的路由或配置),那么即使 Nginx 正确地将请求转发到后端服务器,也可能收到 404 错误。在这种情况下,检查后端服务器的日志可能会提供更多关于为什么请求失败的信息。
    2. 检查日志文件: 检查 Nginx 的访问日志和错误日志(在你的配置中已经指定了路径)。这些日志可能会提供有关问题的详细信息,并有助于确定是哪个部分的配置出现了问题。
    3. 测试其他设置: 如果其他部分(如其他 location 块)依赖于特定的路径或配置,确保它们也配置正确。有时候问题可能与其他部分的配置有关。

    在修改配置后,记得重新加载或重启 Nginx 以应用新的配置。你可以使用命令 nginx -s reload 来重新加载配置,或者使用 nginx -s stopnginx 命令来重启 Nginx。如果问题仍然存在,仔细检查你的配置和日志以找到问题的根源。

    评论

报告相同问题?

问题事件

  • 创建了问题 1月18日