nginx配置失败访问404

nginx.conf配置文件如下:


worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream fileserver{
 server 127.0.0.1:9000 weight=10;
}

upstream gatewayserver{
   server 127.0.0.1:63010 weight=10;
 } 

server {
        listen       80;
        server_name  www.51xuecheng.cn localhost;
        ssi on;
        ssi_silent_errors on;

        location / {
            alias   D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/;
            index  index.html index.htm;
        }
        location /static/img/ {  
                alias  D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/img/;
        } 
        location /static/css/ {  
                alias   D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/css/;
        } 
        location /static/js/ {  
                alias   D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/js/;
        } 
        location /static/plugins/ {  
                alias   D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/plugins/;
                add_header Access-Control-Allow-Origin http://ucenter.51xuecheng.cn;  
                add_header Access-Control-Allow-Credentials true;  
                add_header Access-Control-Allow-Methods GET;
        } 
        location /plugins/ {  
                alias   D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/plugins/;
        } 
       location /course/preview/learning.html {
                alias D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/course/learning.html;
        } 
        location /course/search.html {  
                root   D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal;
        } 
        location /course/learning.html {  
                root   D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal;
        } 


#????????????????????????????????????????????????????
        #api
      location /api/ {
        proxy_pass http://gatewayserver/;
        } 
#openapi
      location /open/content/ {
    proxy_pass http://gatewayserver/content/open/;
      }
      location /open/media/ {
    proxy_pass http://gatewayserver/media/open/;
      }
#????????????????????????????????????????????????????


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



server {
        listen       80;
        server_name  file.51xuecheng.cn;
        #charset koi8-r;
        ssi on;
        ssi_silent_errors on;
        #access_log  logs/host.access.log  main;
        location /video {
            proxy_pass   http://fileserver;
        }

        location /mediafiles {
            proxy_pass   http://fileserver;
        }
   }

}

后nginx.exe -s reload
但是访问http://www.51xuecheng.cn/open/content/course/whole/82
404,nginx的报错日志如下:
[error] 21968#6084: *134 CreateFile() "D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/open/content/course/whole/82" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: www.51xuecheng.cn, request: "GET /open/content/course/whole/82 HTTP/1.1", host: "www.51xuecheng.cn"
可以看出并未走gatewayserver,没有访问127.0.0.1:63010,为什么?如何解决?

根据报错日志,出现了文件路径找不到的错误。错误信息指示了文件路径"D:/downloads/idea-projects/studyOnline_freemarker/xc-ui-pc-static-portal/open/content/course/whole/82"找不到。

根据报错信息中的请求路径"/open/content/course/whole/82"来看,似乎是在访问该路径时出现了问题。然而,从错误信息中并不能直接推断为何没有经过gateway server或者为何没有访问到"127.0.0.1:63010"。

解决这个问题的步骤可能包括:

  1. 检查路径和文件是否存在:确保报错信息中指定的文件路径是正确的,并且确保该文件或路径存在。如果文件或路径不存在,需要确认文件是否被移动或删除。

  2. 检查服务器配置:检查Nginx的配置文件,确认网站的根目录和文件路径的设置是否正确。确保Nginx配置与实际文件路径一致。

  3. 检查网关服务器配置:如果存在网关服务器(如gateway server),需要确认其配置是否正确,并确保请求能够正确地转发到指定的网关服务器。

  4. 检查端口和网络设置:确认网站是否监听了正确的端口,以及网络设置是否正确。确保访问的IP地址和端口与服务器的设置一致。

  5. 检查其他服务和进程:检查其他正在运行的服务和进程,确保它们不会与Nginx或网关服务器产生冲突。

以上是一些可能的解决步骤,但由于问题描述的信息有限,这些步骤可能无法完全解决问题。如果问题仍然存在,建议查阅Nginx的文档或寻求更详细的错误信息来进一步排查和解决问题。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你看下这篇博客吧, 应该有用👉 :nginx重启失败nginx -s reload
  • 除此之外, 这篇博客: nginx 修改配置之后 reload也没有成功加载配置!!!!中的 windows版本nginx  反向代理修改配置后重启不起作用 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    解决方法强制结束nginx.exe进程,但是nginx进程会很多,所以在命令行中输入   taskkill /IM  nginx.exe  /F

    强制结束进程,然后再重启nginx程序就可以了。

    1、查看进程 使用命令:tasklist  /fi "imagename eq nginx.exe"

    2、杀死进程 使用命令:taskkill /IM nginx.exe /F

    3、重启命令

    stop -s nginx 先关闭

    start nginx 启动

    nginx -t 或者 nginx -s reload 重启

  • 以下回答来自chatgpt:

    针对404错误,可能的原因有以下几点: 1. 文件路径错误或文件不存在; 2. Nginx配置文件中location规则匹配不到请求的URL; 3. 服务器中间件或应用程序出错。

    解决方法:

    1. 首先,检查请求的URL路径是否正确,尝试访问其他文件或路径,看是否也出现404错误。如果其他路径可以正常访问,那么问题可能是出现在请求的路径上。需要核对路径是否正确,并确认文件是否存在。

    2. 如果请求的路径没有问题,可能是由于Nginx配置文件中的location规则没有正确配置,导致无法匹配到请求的URL。可以通过以下方式进行调试: a. 在Nginx配置文件中,将access_log设置为debug级别:access_log /var/log/nginx/access.log debug; b. 访问出现404错误的URL; c. 在access.log文件中查找相关信息,找到出现404错误的请求,查看请求的URL路径和匹配到的location规则是否正确。

    3. 如果以上两种方法都没有解决问题,可能是服务器中间件或应用程序出错,需要检查的内容包括: a. 监测nginx进程是否正常启动运行,可以通过任务管理器或命令行进行检查。 b. 检查Nginx的错误日志,看是否有相关的错误信息。 c. 检查应用程序是否正常启动或出现了异常情况。

    以下是一些可能会有用的代码示例: 1. 关闭进程并重新启动Nginx。 taskkill /IM nginx.exe /F # 关闭所有的nginx.exe进程 nginx.exe -c /path/to/nginx.conf # 重新启动Nginx,这里的路径需要按照实际情况进行配置。

    1. 配置Nginx日志格式。 在Nginx配置文件中添加以下配置:
    http {
        log_format main '$remote_addr - $remote_user [$time_local] '
                        '"$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"';
        access_log /var/log/nginx/access.log main;
    }
    

    这样可以将请求IP地址、请求路径、状态码、请求大小、浏览器信息等信息记录在Nginx日志中。

    希望这些解决方案和代码示例能够有所帮助。如果还有问题或需要更多的帮助,请提供更详细的信息,以便我们更好地进行排查和解决。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^