【问题标题】:Nginx reverse proxy to Wordpress with Laravel使用 Laravel 到 Wordpress 的 Nginx 反向代理
【发布时间】:2017-09-06 18:25:01
【问题描述】:

我目前正在管理一个设置,我们在 domain.com 上使用 Laravel webapp,并在 domain.com/blog 上运行 Wordpress 博客。

domain.com/blog 路径被代理到 Wordpress 博客所在的另一台服务器。

设置

服务器 1

基于 Laravel 运行 webapp 的 nginx 网络服务器:

server {
    listen 80;
    server_name default.com;
    return 301 https://www.default.com$request_uri;
}

server {
    listen 443;
    ssl_certificate /etc/letsencrypt/live/www.default.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.default.com/privkey.pem;

    server_name default.com;
    return 301 https://www.default.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl ipv6only=on;

    root /var/www/html/default/current/public;
    index index.php index.html index.htm;

    server_name www.default.com;

    error_log    /var/log/nginx/www.default.com.error.log debug;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /blog/ {
        proxy_pass http://10.2.7.3/blog/;
        proxy_set_header Host $host;
    }

    ssl_certificate /etc/letsencrypt/live/www.default.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.default.com/privkey.pem;
}

服务器 2

运行 Wordpress 的 Apache 网络服务器:

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin webmaster@default.com

        DocumentRoot /var/www/html        

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on
        SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
    </VirtualHost>
</IfModule>

服务器 2 上的目录结构: /var/www/html /var/www/html/blog

Wordpress .htaccess 文件:

RewriteEngine On
RewriteBase /blog
RedirectMatch 301 ^/blog/author/ https://www.default.com/blog
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

问题 博客本身运行良好,所有页面都可见,但 wp-admin/ 重定向到 wp-login.php 失败。

对 wp-admin/ 的 CURL 请求

curl https://www.default.com/blog/wp-admin/
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.10.1
< Content-Type: text/html; charset=UTF-8
< Location: https://www.default.com/blog/wp-login.php?redirect_to=https%3A%2F%2Fwww.default.com%2Fblog%2Fblog%2Fwp-admin%2F&reauth=1

好的,让我们跟随重定向

curl -v 'https://www.default.com/blog/wp-login.php?redirect_to=https%3A%2F%2Fwww.default.com%2Fblog%2Fblog%2Fwp-admin%2F&reauth=1'

这个调用现在由 Laravel 网络应用而不是 Wordpress 博客处理。这不是应该发生的事情。 这是由 nginx 配置引起的:

location / {
     try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

为什么那段配置会覆盖 proxy_pass?

【问题讨论】:

    标签: wordpress apache laravel nginx reverse-proxy


    【解决方案1】:

    nginx 根据各种规则评估location 块为explained by the documentation

    要使location /blog/ 的优先级高于location ~ \.php$,请使用^~ 修饰符:

    location ^~ /blog {
        proxy_pass http://10.2.7.3;
        proxy_set_header Host $host;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      相关资源
      最近更新 更多