【问题标题】:Nginx Configuration for Wordpress AND Laravel Application用于 Wordpress 和 Laravel 应用程序的 Nginx 配置
【发布时间】:2021-05-10 11:53:06
【问题描述】:

我正在尝试让我在 blog.domain.com 上托管的 Wordpress 博客在 domain.com/blog 上工作。

我已经能够使用proxy_pass 成功执行此操作,但问题是某些脚本,例如管理员登录页面 (/wp-login.php) 不起作用 - 它给了我File not found.。我已将问题隔离到我的 Nginx 配置中的 Laravel 应用程序 FastCGI 设置。当我将其注释掉时,我的博客可以工作,但是我的 Laravel 应用程序无法访问。我一直在尝试不同的配置,但我对 Nginx 知之甚少。

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mydomain.com;
    server_tokens off;
    root /home/appfolder/mydomain.com/public;

...

...

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/mydomain.com/server/*;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    
# WHEN I COMMENT OUT THIS BLOCK, BLOG WORKS BUT LARAVEL BREAKS
    location ~ \.php$ {
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_pass unix:/var/run/php/php7.4-fpm-mydomain.sock;
         fastcgi_index index.php;
         include fastcgi_params;
    }
    
    
    location /blog {
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_pass  https://blog.mydomain.com;
    }
    location /blog/wp-content {
        proxy_pass  https://blog.mydomain.com/wp-content;
    } 
    location /blog/wp-includes {
        proxy_pass https://blog.mydomain.com/wp-includes;
    }
    location /blog/wp-content/uploads {
        proxy_pass https://blog.mydomain.com/wp-content/uploads;
    }
    location /blog/wp-login.php {
        proxy_pass https://blog.mydomain.com/wp-login.php;
    }
    location /blog/wp-admin {
        proxy_pass https://blog.mydomain.com/wp-admin;
    }
    location /bitnami {
        proxy_pass https://blog.mydomain.com/bitnami;
    }
    location /mod_pagespeed_beacon {
        proxy_pass https://blog.mydomain.com/mod_pagespeed_beacon;
    }
    
    

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/mydomain.com-error.log error;

    error_page 404 /index.php;



    location ~ /\.(?!well-known).* {
        deny all;
    }
}

...

托管在由 Laravel Forge 管理的 Ubuntu 服务器上。有什么想法吗?

更新:根据理查兹的回答,我已经开始让它工作一点,但我需要为所有看起来效率不高的文件添加规则。

我正在尝试将所有与 Wordpress 相关的 .PHP 文件(在/blog/ 之后)转发到代理。我试过了:

    location ~ /blog/wp-admin/(\d+) {
        add_header X-debug-message "$1";
        proxy_pass https://blog.mydomain.com/wp-admin/$1;
    }

但这似乎不起作用。

【问题讨论】:

  • 正则表达式 location 优先于前缀 location,除非您使用^~ 运算符。见this document
  • @RichardSmith 谢谢!我将 '=' 运算符添加到 wp-login.php 位置,该位置有效,但有许多 .php 文件,所以我将如何使其更具动态性。例如,'wp-admin/edit.php' -> File not found' 我遇到了同样的问题。 \

标签: laravel nginx nginx-config


【解决方案1】:

我通过创建一个正则表达式来匹配/wp-admin/ 之后的任何内容,然后将其转发到代理。我使用的配置是:

location ~ (blog\/wp-admin)\/(.*)\.php$ {
    proxy_pass https://blog.mydomain.com/wp-admin/$2.php?$query_string;
}

$2 是第二个捕获组 (.*),它附加到 proxy_pass URL 并使用 ?$query_string 添加任何其他参数。

【讨论】:

    猜你喜欢
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2017-01-17
    • 2021-01-30
    • 2015-01-05
    • 2022-06-23
    • 1970-01-01
    相关资源
    最近更新 更多