所以我有一个我一直在试验的树莓派网络服务器,它运行 nginx 来为多个站点等提供服务。我想在 docker 容器中作为博客运行 wordpress,但我在正确配置 nginx+docker wordpress 设置时遇到问题。
这是我的 docker-compose.yml:
version: "3"
services:
db:
image: hypriot/rpi-mysql
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: <password>
networks:
- wp
wordpress:
depends_on:
- db
image: wordpress
restart: always
volumes:
- ./:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: <password>
ports:
- 8082:80
networks:
- wp
networks:
wp:
volumes:
db_data:
这是我当前的 example.com 的 nginx .conf:
server {
client_max_body_size 32M;
# Listen HTTP
listen 80;
server_name www.example.com example.com;
# Redirect HTTP to HTTPS
return 301 https://$host$request_uri;
}
server {
client_max_body_size 32M;
# Listen HTTP
listen 443 ssl;
server_name example.com www.example.com;
# SSL config
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# does not fix the issue
port_in_redirect off;
# Proxy Config
location / {
# My attempts at fixing the port issue (did not work in any combination)
proxy_bind $host:443;
proxy_redirect off;
port_in_redirect off;
absolute_redirect off;
proxy_set_header Location $host:443;
proxy_set_header Host $http_host:443;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8082/;
# an extra try despite my 8082 port not being open
proxy_redirect https://example.com:8082/ https://example.com/;
}
# testing and looking at just the /wp-login.php "works" but without any of the content
location ~ \.php {
proxy_pass http://127.0.0.1:8082;
}
}
我的问题:访问我的 example.com
域后,我被重定向到 example.com:8082
并且没有获取任何内容,而且我已经很多问题试图找出解决方法。我也尝试过在端口 80 上使用 http,但这并没有什么区别(除非我在本地网络上,它在本地获取文件)
上面的 nginx 设置中是否缺少一些简单的东西?
有没有办法让docker在不同的虚拟端口上转发它?
最佳答案
好吧,看来问题不在于 docker/nginx 设置,而在于 wordpress。我错误地通过 [rpi.local.ip.address]:8082 填写了初始 WordPress 设置,并将其保存在配置中。
我最终只是使用docker-compose down --volumes
重置了卷,尽管这会删除您的所有数据。
真正的答案是这里找到的问题的解决方案: Docker: I can't map ports other than 80 to my WordPress container
我还对文件进行了一些修改,因此有效的文件如下:
如果这些也不适合您,那么您可以:
- 使用
docker-compose down --volumes
重置容器, - 删除
端口:- 8082:80
- 使用
proxy_pass http://[docker-ip]:80/; 转发到
docker Inspect [id-of-wordpress-container]
找到的 IP 地址;
然后设置 WordPress 安装,并且仅在重新添加端口:- 8082:80
后进行,因为此 IP 在重新启动后可能会更改
docker-compose.yml
version: "3"
services:
db:
image: mysql/mysql-server:8.0
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: VNz5EHiZkec9mn
networks:
- wp
command: '--default-authentication-plugin=mysql_native_password'
wordpress:
depends_on:
- db
image: wordpress
restart: always
volumes:
- ./wp-content/:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: VNz5EHiZkec9mn
networks:
- wp
ports:
- 8082:80
networks:
wp
volumes:
db_data:
/etx/nginx/sites-available/example.com.conf
添加了一个重定向,以防 301 重定向缓存在浏览器中
server {
client_max_body_size 32M;
# Listen HTTP
listen 80;
server_name www.example.com example.com;
# Redirect HTTP to HTTPS
return 301 https://$http_host$request_uri;
}
server {
listen 8082 ssl;
server_name example.com www.example.com;
# SSL config
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://scienceangles.com;
}
server {
client_max_body_size 32M;
# Listen HTTP
listen 443 ssl;
server_name example.com www.example.com;
# SSL config
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_stapling on;
ssl_stapling_verify on;
port_in_redirect off;
# Proxy Config
location / {
proxy_pass http://localhost:8082;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
关于wordpress - 如何将 nginx 反向代理修复为用户的 WordPress docker 重定向端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62925541/