如何通过 Nginx 从 Docker 代理网站,而无需它充当本地主机?
我当前的设置包括一个 Ubuntu 主机和几个带有公开端口的 Docker 容器。Docker 容器仅在本地公开。互联网上的公开包括一个 Nginx 服务器,该服务器将 Docker 容器代理到某些子域。
我在一个 Docker 容器中运行 Wordpress。我从Docker 中心使用这个 YML 文件:
version: '3.1'
services:
wordpress:
container_name: myapp-cms
image: wordpress
restart: always
ports:
- 8087:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- /srv/wordpress/cms:/var/www/html
db:
container_name: myapp-db
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- /srv/wordpress/db:/var/lib/mysql
这个 Docker 容器暴露到内部端口 8087,然后我将其代理到mysubdomain.mydomain.com
。这是 Nginx 配置文件:
server {
root /var/www/mysubdomain.mydomain.com;
index index.html index.htm index.nginx-debian.html;
server_name mysubdomain.mydomain.com;
client_max_body_size 1000M;
location / {
# try_files $uri $uri/ =404;
proxy_pass http://localhost:8087;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mydomain.com-0003/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain.com-0003/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 = mysubdomain.mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name mysubdomain.mydomain.com;
return 404; # managed by Certbot
}
问题是,页面(在本例中为 Wordpress)仍然使用localhost:8087
每个链接。这破坏了点击某些内容的所有可能性,也破坏了 CSS 和 JavaScript 库加载。
编辑1:
我添加了以下两行wp-config.php
:
define('WP_HOME','https://mysubdomain.mydomain.com');
define('WP_SITEURL','https://mysubdomain.mydomain.com');
它成功加载了设置页面,并适配了域名。我可以成功设置 WordPress,但只要我保存设置(用户名、密码、电子邮件),浏览器就会加载一个空白页面。调用curl -X GET https://mysubdomain.mydomain.com
返回一个空响应。