[英]Nginx with docker: Reverse Proxy doesn't work
I recently want to set a reverse proxy server.最近想设置一个反向代理服务器。 I pulled a nginx docker image and I ran the docker container using this command:我拉了一个 nginx docker 镜像,并使用以下命令运行了 docker 容器:
docker run -d --name ngtest -p 4080:80 nginx
Then I updated the /etc/nginx/nginx.conf like below:然后我更新了 /etc/nginx/nginx.conf 如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server{
listen 80;
location /nexus/ {
proxy_pass http://192.168.0.30:8081/;
}
}
}
The container works after restarted, when I tried to open page http://192.168.0.30:4080 , I got the default Nginx page.容器在重新启动后工作,当我尝试打开页面http://192.168.0.30:4080 时,我得到了默认的 Nginx 页面。
However when I tried this url: http://192.168.0.30:4080/nexus , I got a 404 page with errors.但是,当我尝试这个 url: http://192.168.0.30:4080/nexus 时,我得到了一个 404 页面,但有错误。
From the log, it looks that the nginx didn't forward the url to the page I set in nginx.conf, instead it tried to look the page in the local directory:从日志中可以看出,nginx 没有将 url 转发到我在 nginx.conf 中设置的页面,而是尝试在本地目录中查看该页面:
2020/03/11 16:10:34 [error] 6#6: *251 open() "/usr/share/nginx/html/nexus" failed (2: No such file or directory), client: 192.168.0.153, server: localhost, request: "GET /nexus HTTP/1.1", host: "192.168.0.30:4080"
192.168.0.153 - - [11/Mar/2020:16:10:34 +0000] "GET /nexus HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36" "-"
Is there anything wrong in my steps?我的步骤有什么问题吗?
Thanks,谢谢,
Alex亚历克斯
The issue is that even with your updated config you are still doing:问题是,即使你更新了配置,你仍然在做:
include /etc/nginx/conf.d/*.conf;包括/etc/nginx/conf.d/*.conf;
Which is loading /etc/nginx/conf.d/default.conf
which will be the default server in the event no server name is provided based on the rules of determining the server to use if server name not found.正在加载/etc/nginx/conf.d/default.conf
,如果未找到服务器名称,则根据确定要使用的服务器的规则,如果没有提供服务器名称,它将成为默认服务器。
You should do something like:你应该做这样的事情:
Save to a config file your server definition of:将您的服务器定义保存到配置文件:
server{
listen 80;
location /nexus/ {
proxy_pass http://192.168.0.30:8081/;
}
}
Then run然后运行
docker run -d -p 4080:80 -v (path_to_your_config):/etc/nginx/conf.d/default.conf nginx
This will replace the default config with yours.这将用您的配置替换默认配置。 Then if you need to make changes you just make them and restart the container.然后,如果您需要进行更改,只需进行更改并重新启动容器即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.