在 Nginx 中设置 Wordpress 和 Rails

mak*_*han 5 php wordpress fastcgi ruby-on-rails nginx

我为 Rails 应用程序设置了 nginx,它对我来说工作得很好。现在我想将我的 Wordpress 博客从 移动blog.website.comwebsite.com/blog,以便网络爬虫将其视为网站的一部分。

我在 Rails 应用程序的public/目录中创建了一个符号链接,并将以下内容添加到我的 nginx 配置中:

# Rails server
server {
    root /project/path/current/public;
    server_name project.com;
    passenger_enabled on;
    rails_env production;
    client_max_body_size 20m;

    if ($http_x_forwarded_proto != 'https') {
        return 301  https://$host$request_uri;
    }

    location /blog {
        index    index.html  index.php;
        try_files $uri $uri/ /blog/index.php?q=$uri&$args;

        if (!-e $request_filename) {
            rewrite ^.+?(/blog/.*.php)$ $1 last;
            rewrite ^ /blog/index.php last;
        }
    }

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_intercept_errors on;
    include fastcgi_params;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}
Run Code Online (Sandbox Code Playgroud)

这很好用。但我想做的是跳过符号链接部分。我希望 nginx 直接路由到 wordpress 目录。我尝试将/blog块更改为:

location ^~ /blog {
  root /home/ubuntu/apps/blog;
  index    index.html  index.php;
  try_files $uri $uri/ /blog/index.php?q=$uri&$args;

  if (!-e $request_filename) {
    rewrite ^.+?(/blog/.*.php)$ $1 last;
    rewrite ^ /blog/index.php last;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,就像我被正确重定向到 wordpress 文件夹,但我的服务器不知道如何处理 php 文件并将它们作为要下载的文件发送到我的浏览器。

我尝试将\.php$位置嵌套在位置内/blog,但会导致 404 错误。

下面的部分有什么问题以及如何修复它?

location ^~ /blog {
  root /home/ubuntu/apps/blog;
  index    index.html  index.php;
  try_files $uri $uri/ /blog/index.php?q=$uri&$args;

  if (!-e $request_filename) {
    rewrite ^.+?(/blog/.*.php)$ $1 last;
    rewrite ^ /blog/index.php last;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_intercept_errors on;
    include fastcgi_params;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
  }
}
Run Code Online (Sandbox Code Playgroud)

mak*_*han 2

我还没有修复问题中的代码片段,但我设法通过使用两个server块来解决 nginx 配置的问题,而无需使用符号链接。

server {
    location / {
        proxy_pass http://localhost:82/;
    }

    location /blog {
        root /var/proj/blog/current;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}
# Rails server                                                                                                                                                                                                                             
server {
    listen 82;
    root /var/proj/site/current/public;
    server_name site.com;
    passenger_enabled on;
    rails_env staging;
    client_max_body_size 20m;

    # other configuration
}
Run Code Online (Sandbox Code Playgroud)