Nginx

使用多個 wordpress 站點配置 nginx 多個子文件夾時出錯

  • February 18, 2018

我在為多個子文件夾配置 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;
#   }
#}

我會設置一個反向代理並通過它為兩個 WordPress 網站提供服務。此設置的邏輯是:

  • 在您的主 vhost 文件(這將是反向代理)中,將伺服器設置為使用 server_name optopshare.com 在公共 IP 上列出。
  • 在其中添加 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 文件。
  • 使用偵聽 127.0.0.1:8001 的伺服器創建第二個 vhost 文件,以服務於 WordPress 主站點。
  • 使用偵聽 127.0.0.1:8002 的伺服器創建第三個 vhost 文件,用於為 /blog WordPress 站點提供服務。

請記住,如果在反向代理後面提供 WordPress,則需要注意一些事項,具體取決於您的個人設置。例如:

  • 要讓 WordPress 在反向代理後使用 SSL 執行,您需要將其添加到 wp-config.file:
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
   $_SERVER['HTTPS']='on';
}

引用自:https://serverfault.com/questions/896573