【问题标题】:Nginx 404 not foundNginx 404 未找到
【发布时间】:2021-06-03 01:09:20
【问题描述】:

我正在使用 docker compose 运行最新版本的 nginx,使用卷我将 nginx.conf 文件复制到 nginx docker 容器中

  nginx:
    image: nginx:1.20
    container_name: nginx
    ports:
      - 80:80  
    restart: unless-stopped
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/default.conf
    depends_on:
      - strapi
      - rocketchat
      - keycloak
    networks:
      - test-network

每个应用程序都在同一个网络上运行。

这里是 nginx.conf 文件

events {
  worker_connections  4096;  ## Default: 1024
}
http {
 server {
   listen 80;

   server_name qa.xxx.com;

   location / {
       proxy_pass http://strapi-container:1337/;
   }

   location /chat {
       proxy_pass http://rocketchat-container:3000;

   }

   location /auth {
       proxy_pass http://keycloak-container:8080;
       proxy_set_header Host $host;
   }
 }
}

我的意图是使用 nginx 配置运行三个后端 URL /、/chat、/auth。在实例上运行应用程序时,http://ip-address/chat、http://ip-address/auth 似乎不起作用

这里是 nginx 日志错误

2021/06/02 07:46:42 [error] 31#31: *1 open() "/usr/share/nginx/html/chat" failed (2: No such file or directory), client: 115.96.103.237, server: localhost, request: "GET /chat HTTP/1.1", host: "310.28.67.222"
115.96.103.237 - - [02/Jun/2021:07:46:42 +0000] "GET /chat HTTP/1.1" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" "-"
2021/06/02 07:46:50 [error] 31#31: *2 open() "/usr/share/nginx/html/auth" failed (2: No such file or directory), client: 115.96.103.237, server: localhost, request: "GET /auth HTTP/1.1", host: "310.28.67.222"
115.96.103.237 - - [02/Jun/2021:07:46:50 +0000] "GET /auth HTTP/1.1" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" "-"

【问题讨论】:

  • 使用nginx -T(大写T)查看所有包含文件的完整配置。错误信息与您发布的配置文件不一致。该错误对应于server_name localhostnot server_name qa.xxx.com

标签: docker nginx nginx-reverse-proxy nginx-config nginx-location


【解决方案1】:

尝试在 proxy_pass 后面加上斜杠,除非您想添加文件夹。

 location /chat {
       proxy_pass http://rocketchat-cnr:3000;
   }

重定向到 http://rocketchat-c​​nr:3000/chat,同时

 location /chat {
       proxy_pass http://rocketchat-cnr:3000/;
   }

重定向到 http://rocketchat-c​​nr:3000/

访问http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass了解更多信息

【讨论】:

  • 我添加了斜杠并执行了,但ip-address/chat 不起作用。
  • 请发布完整的撰写
  • 如果有人使用 IP 地址而不是服务器名称发出请求,“Host”请求头字段将包含 IP 地址,并且可以使用 IP 地址作为服务器名称来处理请求:服务器{听80; server_name example.org www.example.org "" 192.168.1.1 ; ... } nginx.org/en/docs/http/server_names.html
  • @arsisch 谢谢,覆盖 nginx 默认配置后它工作了
猜你喜欢
  • 2016-06-23
  • 2016-12-27
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 2018-05-03
相关资源
最近更新 更多