【问题标题】:Nginx configuration for Wordpress in a directory目录中 Wordpress 的 Nginx 配置
【发布时间】:2016-11-03 04:07:54
【问题描述】:

我有一个使用 nginx 配置的 Rails 应用程序,其根目录位于 /var/www/apps/example/current/public。访问地址为https://www.example.com。这一切都很好。我决定添加一个博客,我想使用 Wordpress。我将它安装在服务器上的另一个目录 /var/www/apps/blog 中,我希望能够通过转到 https://www.example.com/blog 来访问它

下面是我的 nginx 配置:

server {
  listen      443;
  ssl on;
  ssl_certificate    /etc/ssl/certs/example.com.pem;
  ssl_certificate_key    /etc/ssl/certs/example.com.key;

  server_name www.example.com;
  root         /var/www/apps/example/current/public;
  passenger_enabled on;
  rails_env production;

  client_max_body_size 100M;
  client_body_buffer_size 256k;

  error_page  404              /404.html;

  error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
  }

  location /blog {
    root /var/www/apps;

    location ~ [^/]\.php(/|$) {
      try_files $uri =404;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
    }
  }
}

我可以导航到 example.com/blog,它将我带到那里没有任何问题。当我启用漂亮的 URL 并尝试查看帖子时,问题就出现了。它绕过 /blog 目录并访问了我的 Rails 应用程序,以 404 结束。我在 /var/log/nginx/error.log 中看到以下错误:

2016/06/30 17:24:32 [error] 7035#0: *20 "/var/www/apps/blog/hello-world/index.html" is not found (2: No such file or directory), client: 199.27.128.198, server: www.example.com, request: "GET /blog/hello-world/ HTTP/1.1", host: "www.example.com", referrer: "https://www.example.com/blog/"

添加index index.php 指令并不能解决问题,它只是说明没有找到/var/www/apps/blog/hello-world/index.php

有什么想法吗?我被难住了。

【问题讨论】:

    标签: ruby-on-rails wordpress nginx


    【解决方案1】:

    try_files $uri=404; 对于漂亮的永久链接来说是不够的。

    要启用漂亮的永久链接,您需要将其更改为:

    location /blog {
      root /var/www/apps/blog;
    
      try_files $uri $uri/ /index.php?$args;
    
      location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
    }
    

    或者,试试non-root try_files redirect

    location /blog {
      try_files $uri $uri/ /blog/index.php?$args;
    
      location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
    } 
    

    【讨论】:

    • 足够接近,我会将其标记为解决方案,因为它为我指明了正确的方向。只需在您给出的第一个示例中添加 try_files $uri $uri/ /blog/index.php?$args; 即可。
    【解决方案2】:
    猜你喜欢
    • 2015-05-21
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2013-09-05
    • 1970-01-01
    相关资源
    最近更新 更多