我正在尝试使用docker-compose开发Nginx反向代理。我可能无法正确说明问题,但我正在发布代码和错误。当我尝试代理一项服务时,代理有效,但浏览器上未显示任何内容。
这是我的nginx.conf:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream docker-nginx {
server react-app:80;
}
server {
listen 62106;
server_name http://10.1.40.24;
location /try {
rewrite ^/try(.*)$ $1 break;
proxy_pass http://docker-nginx/;
}
}
}
这是我的docker-compose文件:
version: '3.5'
services:
react-app:
build:
context: ./my-app
cache_from:
- nginx:alpine
ports:
- "62101:80"
image: app-react-uat:latest
networks:
my-network:
aliases:
- perfreview
server-app:
build:
context: ./Flask
cache_from:
- python:3-slim
environment:
- ENV = production
- PORT = 62102
ports:
- "62102:62102"
image: flask-py-app-uat:latest
nginx-reverse:
build:
context: ./nginx
cache_from:
- nginx:alpine
ports:
- "62106:62106"
depends_on:
- react-app
networks:
- my-network
networks:
my-network:
这是错误消息:
react-app_1 | 172.28.0.3 - - [03/Sep/2019:04:07:03 +0000] "GET / HTTP/1.0" 200 333 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76. 0.3809.132 Safari/537.36" "-"
nginx-reverse_1 | 10.1.20.45 - - [03/Sep/2019:04:07:03 +0000] "GET /try/ HTTP/1.1" 200 333 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome /76.0.3809.132 Safari/537.36"
nginx-reverse_1 | 2019/09/03 04:07:03 [error] 6#6: *12 open() "/etc/nginx/html/static/css/main.c0c280ad.css" failed (2: No such file or directory), client: 10.1.20.45, server: http:// 10.1.40.24, request: "GET /static/css/main.c0c280ad.css HTTP/1.1", host: "10.1.40.24:62106", referrer: "http://10.1.40.24:62106/try/"
nginx-reverse_1 | 10.1.20.45 - - [03/Sep/2019:04:07:03 +0000] "GET /static/css/main.c0c280ad.css HTTP/1.1" 404 555 "http://10.1.40.24:62106/try/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
nginx-reverse_1 | 2019/09/03 04:07:03 [error] 6#6: *13 open() "/etc/nginx/html/static/js/main.5a0e26e5.js" failed (2: No such file or directory), client: 10.1.20.45, server: http://10 .1.40.24, request: "GET /static/js/main.5a0e26e5.js HTTP/1.1", host: "10.1.40.24:62106", referrer: "http://10.1.40.24:62106/try/"
nginx-reverse_1 | 10.1.20.45 - - [03/Sep/2019:04:07:03 +0000] "GET /static/js/main.5a0e26e5.js HTTP/1.1" 404 555 "http://10.1.40.24:62106/try/" "Mozilla/5.0 (Windows NT 10.0; Win64; x 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
nginx-reverse_1 | 10.1.20.45 - - [03/Sep/2019:04:07:30 +0000] "GET /static/css/main.c0c280ad.css HTTP/1.1" 404 555 "http://10.1.40.24:62106/try/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
nginx-reverse_1 | 2019/09/03 04:07:30 [error] 6#6: *13 open() "/etc/nginx/html/static/css/main.c0c280ad.css" failed (2: No such file or directory), client: 10.1.20.45, server: http:// 10.1.40.24, request: "GET /static/css/main.c0c280ad.css HTTP/1.1", host: "10.1.40.24:62106", referrer: "http://10.1.40.24:62106/try/"
任何帮助将不胜感激。谢谢!
最佳答案
因此,我找出了错误的原因以及解决方法。基本上,react-app容器的文件未与反向代理容器共享。因此,我使用卷来解决这个问题,并在配置文件的位置部分中为安装部分的位置设置别名。这是新的nginx.conf:
server {
listen 62106;
server_name http://10.1.40.24;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location /perfreview/ {
alias /usr/share/nginx/html/prs;
proxy_pass http://react-app:80/;
}
#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 /usr/share/nginx/html;
}
}
希望有一天能对某人有所帮助!
关于docker - 反向代理Nginx不代理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57765028/