我正在尝试在Nginx服务器上使用docker容器运行WordPress应用。
我使用以下docker-compose.yml
文件在本地运行WordPress
version: "3"
services:
wordpress_app:
image: wordpress:latest
restart: always
container_name: wordpress_app
environment:
WORDPRESS_DB_HOST: mysql_server:3306
WORDPRESS_DB_USER: db_username
WORDPRESS_DB_PASSWORD: db_password
WORDPRESS_DB_NAME: wordpress_app
depends_on:
- mysql_server
volumes:
- wordpress_app_public_html:/var/www/html
ports:
- "50001:80"
- "50002:443"
mysql_server:
image: mysql:latest
restart: always
container_name: mysql_server
environment:
MYSQL_DATABASE: wordpress_app
MYSQL_USER: db_username
MYSQL_PASSWORD: db_password
MYSQL_ROOT_PASSWORD: db_root_password
volumes:
- mysql_server_data:/var/lib/mysql
volumes:
wordpress_app_public_html:
mysql_server_data:
networks:
default:
external:
name: wordpress-network
现在,我添加了一个名为/etc/nginx/sites-available/usa.mydomain.com.conf
的文件,其中包含以下内容
server {
listen 80;
server_name usa.mydomain.com;
index index.php;
access_log /var/log/nginx/usa.mydomain.com-access.log;
error_log /var/log/nginx/usa.mydomain.com-error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass locahost:50001;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
然后我将具有相同配置的虚拟文件添加到sites-enabled
文件夹中,像这样
cd /etc/nginx/sites-enabled
ln -s ../sites-available/usa.mydomain.com.conf .
但是当我打开usa.mydomain.com时出现错误
问题加载页面:连接已重置
但是当我将配置更改为以下内容时,该站点可以正常工作。但是html代码将内容解析为http://localhost:50001
server {
listen 80;
server_name usa.mydomain.com;
location / {
proxy_pass http://localhost:50001;
}
index index.php;
access_log /var/log/nginx/usa.mydomain.com-access.log;
error_log /var/log/nginx/usa.mydomain.com-error.log;
}
因此页面HTML源代码如下
<!DOCTYPE html>
<html lang="en-US" xml:lang="en-US">
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,nofollow" />
<title>WordPress › Installation</title>
<link rel='stylesheet' id='dashicons-css' href='http://localhost:50001/wp-includes/css/dashicons.min.css?ver=5.5.1' type='text/css' media='all' />
<link rel='stylesheet' id='buttons-css' href='http://localhost:50001/wp-includes/css/buttons.min.css?ver=5.5.1' type='text/css' media='all' />
<link rel='stylesheet' id='forms-css' href='http://localhost:50001/wp-admin/css/forms.min.css?ver=5.5.1' type='text/css' media='all' />
<link rel='stylesheet' id='l10n-css' href='http://localhost:50001/wp-admin/css/l10n.min.css?ver=5.5.1' type='text/css' media='all' />
<link rel='stylesheet' id='install-css' href='http://localhost:50001/wp-admin/css/install.min.css?ver=5.5.1' type='text/css' media='all' />
如何正确配置nginx网站,以便WordPress可以正常工作?
答案 0 :(得分:0)
我终于弄清楚了问题所在。添加proxy_set_header HOST $host;
是缺少的和平。下面是我的最终/工作配置。
server {
listen 80;
server_name usa.mydomain.com;
root /data/wordpress_app/public_html;
access_log off;
error_log /var/log/nginx/wordpress_app-error.log;
index index.html index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location / {
# First attempt to serve request as file, then as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
proxy_pass http://localhost:8000;
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-Proto $scheme;
}
}