【发布时间】:2019-06-13 04:43:51
【问题描述】:
我有一个运行 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 配置中,但我不确定我做错了什么?任何帮助都会很棒。
【问题讨论】:
标签: docker nginx reverse-proxy lets-encrypt