设置为反向代理时,Nginx服务器返回404静态文件

owfi6suc  于 2023-10-17  发布在  Nginx
关注(0)|答案(2)|浏览(168)

两个docker容器运行两个网站,apache作为服务器在端口60000和60002 nginx docker(使用nginx docker镜像创建)作为反向代理托管在端口61001 ip地址为10.0.2.15当http://10.0.2.15:61001/app1被调用时,检查网络后返回一个空白网站,返回502 bad gateway

server {
    listen 80;
    server_name 10.0.2.15;

    location /app1/ {
        proxy_pass http://10.0.2.15:60000/;
        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;
    }

    location /app2/ {
        proxy_pass http://10.0.2.15:60002/;
        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;
    }
}

这是nginx docker容器中的默认.conf文件
理想情况下,它应该去http://10.0.2.15:61001/app1/static/的所有内容和React的应用程序都是应用程序的路线,以及或有任何更好的方式实现这个

2nbm6dog

2nbm6dog1#

http {

server {

    server_name 10.0.2.15;
    listen 80;
    listen [::]:80;
    
    location /app1 {
        set $url "http://10.0.2.15:60000"; # or try your lan ip
    }
    location /app2 {
        set $url "http://10.0.2.15:60002"; # or try your lan ip
    }
     }
}

你可以试试这个

yqkkidmi

yqkkidmi2#

您无法通过端口61001建立连接来侦听端口80。您需要监听端口61001
你需要配置nginx设置如下:

server {
    listen 61001;
    server_name 10.0.2.15;

    location /app1/ {
        proxy_pass http://10.0.2.15:60000/;
        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;
    }

    location /app2/ {
        proxy_pass http://10.0.2.15:60002/;
        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;
    }
}

相关问题