我有一台运行Nginx和Docker的Ubuntu服务器。我的Docker容器正在端口4200上运行。
docker run -d -p4200:4200 my-app:latest
我可以确认它正在运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5be8856b9e2 my-app:latest "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 80/tcp, 0.0.0.0:4200->4200/tcp hopeful_diffie
我已经像这样设置了我的Nginx默认配置文件:
server {
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name my-app.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://localhost:4200/;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/my-app.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/my-app.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name www.my-app.com; # managed by Certbot
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://localhost:4200/;
}
listen [::]:443 ssl ; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/my-app.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/my-app.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = my-app.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [:
:]:80 default_server;
server_name my-app.com;
return 404; # managed by Certbot
}
server {
if ($host = www.my-app.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
listen [::]:80 ;
server_name www.my-app.com;
return 404; # managed by Certbot
}
在尝试添加proxy_pass之前(将proxy_pass行切换到try_files $ uri $ uri / = 404;),我正在获取默认的nginx页面。我希望添加proxy_pass行将请求转发到运行Docker容器的服务器端口4200。相反,我得到了502 Bad Gateway。我以为问题出在我的Ngnix配置中,但我不确定自己做错了什么?任何帮助都会很棒。
答案 0 :(得分:0)
这对我来说是一个愚蠢的错误,但这也许会在将来对某人有所帮助。它在我的docker run命令中。
docker run -d -p 4200:80 my-app:latest
我的Docker容器内部正在监听80,但我将其发送到4200。