nginx的location配置导致网关返回404问题

 更新时间:2024年06月20日 08:49:10   作者:飘零未归人  
这篇文章主要介绍了nginx的location配置导致网关返回404问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

nginx的location配置导致网关返回404

再项目中使用nginx转发到网关时,发现返回了404.

{

    "timestamp": "2023-11-01T02:12:48.788+00:00",

    "path": "//core-manage-web/core/core-manage/servers/findServers",

    "status": 404,

    "error": "Not Found",

    "message": null,

    "requestId": "642b125b"

}

从这个返回来看,应该是网关返回的信息。因为如果是nginx返回404的话,应该是返回的404.html才对。

所以看看出是网关找不到转发的路径。 从  "path": "//core-manage-web/core/core-manage/servers/findServers",看出我们的接口经过nginx转发之后,居然只有两个//。正常应该只有一个才对。

再看一下location配置:

location /core {
    proxy_pass http://gateway-upstream/;
}

由于第一次使用使用nginx,所以对于这些配置还不是很了解。这上面的的  “location /core”标识路径前缀匹配。 而我的 “http://core-gateway-upstream/”是以“/”结尾,这表示会将 location的匹配路径(/core)替换掉在转发。

gateway-upstream配置如下

upstream gateway-upstream {
    server 192.168.111.1:10006;
}

所以如果我是用 http:localhost:8080/core/login/xxxxx访问,则经过nginx转发之后会变成192.168.111.1:10006//login/xxxxx

所以需要将 location /core改成 location /core/。即可:

location /core/ {
    proxy_pass http://gateway-upstream/;
}

nginx配置多个location访问报404

解决方法

在自己配置的location中不要使用root配置文件目录,替换为alias即可

完整配置

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

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            autoindex on;             #开启索引功能
            autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
            autoindex_localtime on;   # 显示本机时间而非 GMT 时间
            charset utf-8; # 避免中文乱码
            root   file;
            #index  index.html index.htm;
        }
        
        location /file {
	    alias   html;
            index  index.html index.htm;
	}
        #error_page  404              /404.html;

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

}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • nginx proxy_set_header的具体实现

    nginx proxy_set_header的具体实现

    proxy_set_header 是 Nginx 配置中的一个重要指令,本文主要介绍了nginx proxy_set_header的具体实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-07-07
  • web部署到nginx以后js,css等静态文件加载不正常的解决过程

    web部署到nginx以后js,css等静态文件加载不正常的解决过程

    在Ubuntu中配置Web页面时,JS和CSS文件虽可获取但加载失败,可能因路径错误、服务器配置不当、权限限制或缓存问题导致页面布局异常,需检查文件路径、服务器设置、MIME类型及浏览器控制台错误信息以排查原因
    2025-09-09
  • nginx设置超时时间的问题及解决方案

    nginx设置超时时间的问题及解决方案

    程序在处理大量数据,接口超过1分钟(默认的)未返回数据,导致等待超时,出现这种情况,我们可以先优化程序,缩短执行时间,可以调大nginx超时限制的参数,使程序可以正常执行,本文介绍nginx设置超时时间及504 Gateway Time-out的问题解决方案,一起看看吧
    2024-02-02
  • Nginx 禁止直接访问目录或文件的操作方法

    Nginx 禁止直接访问目录或文件的操作方法

    Nginx 默认是不允许列出整个目录的,那么需要这样的功能怎么操作呢,下面小编给大家介绍下Nginx 禁止直接访问目录或文件的方法,需要的朋友可以参考下
    2022-10-10
  • Nginx配置后请求报404的几种问题解决方法

    Nginx配置后请求报404的几种问题解决方法

    本文主要介绍了Nginx配置后请求报404的两种常见问题及其解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-02-02
  • nginx配置相关介绍

    nginx配置相关介绍

    本文为大家介绍一下nginx配置的相关介绍,供大家参考
    2013-02-02
  • nginx proxy_pass 路径拼接的具体实现

    nginx proxy_pass 路径拼接的具体实现

    本文主要介绍了nginx proxy_pass 路径拼接的具体实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-05-05
  • windows下RunHiddenConsole 启动 nginx与php(RunHiddenConsole下载)

    windows下RunHiddenConsole 启动 nginx与php(RunHiddenConsole下载)

    这篇文章主要介绍了RunHiddenConsole 启动 nginx与php的相关资料,希望通过本文能帮助到大家,让大家学会使用RunHiddenConsole,需要的朋友可以参考下
    2017-10-10
  • nginx 配置服务启动的教程详解

    nginx 配置服务启动的教程详解

    本文给大家详细介绍了nginx 配置服务启动的相关知识,非常不错,具有一定的参考借鉴价值,感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-05-05
  • Nginx进程管理和重载原理详解

    Nginx进程管理和重载原理详解

    这篇文章主要给大家介绍了关于Nginx进程管理和重载原理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04

最新评论