我在为多个子文件夹配置 nginx 时遇到问题,我已在 serverfault 上应用了许多解决方案,但对我的网站不起作用。我的网站使用两个 wordpress。主网站https://optshare.com以及博客网站:https://optshare.com/blog
我在根文件夹 /var/www/html/blog 中创建了一个子文件夹 blog
这是我的 nginx 配置
主网站可以运行,但博客网站无法运行
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name optshare.com www.optshare.com;
return 301 https://$server_name$request_uri;
}
server {
# SSL configuration
#
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-optshare.com.conf;
include snippets/ssl-params.conf;
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name optshare.com www.optshare.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location /blog {
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php$is_args$args;
}
location /en {
index index.php index.html index.htm;
try_files $uri $uri/ /en/index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location ~ /.well-known {
allow all;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
答案1
我将设置一个反向代理并通过它为两个 WordPress 网站提供服务。此设置的逻辑如下:
- 在您的主 vhost 文件中(这将是反向代理),将服务器设置为在公共 IP 上列出,服务器名称为 optopshare.com。
- 在其中添加 http->https 重定向和 SSL 处理(因为您已经这样做了)。
添加两个位置部分
location / { proxy_set_header X-Original-Request $request_uri; 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; proxy_pass http:127.0.0.1:8001; } location /blog/ { proxy_set_header X-Original-Request $request_uri; 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; proxy_pass http:127.0.0.1:8002; }
这就是主 vhost 文件。
创建第二个 vhost 文件,并使用监听 127.0.0.1:8001 的服务器为 WordPress 主站点提供服务。
- 创建第三个 vhost 文件,并使用服务器监听 127.0.0.1:8002,为 /blog WordPress 网站提供服务。
请记住,如果使用反向代理为 WordPress 提供服务,则需要注意一些事项,具体取决于您的个人设置。例如:
要使 WordPress 在反向代理后面使用 SSL 运行,您需要将其添加到您的 wp-config.file 中:
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { $_SERVER['HTTPS']='on'; }
答案2
感谢 Bob 的热情帮助!反向代理对我来说实际上很难设置,我尝试了很多次但总是显示错误。我找到了另一个解决方案,不需要使用反向代理,我只需将下面的代码添加到 nginx 服务器块中
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^(/[^/]+)?(/wp-.*) $2 last;
rewrite ^(/[^/]+)?(/.*\.php) $2 last;
}
对于大型网站,我认为反向代理是最好的解决方案