duanhu2414 2014-11-24 12:57
浏览 47
已采纳

nginx无法为wordpress处理php

I have a rails app running at say "myapp.com". I tried to load wordpress at "myapp.com/blog". I started off with an nginx configuration that looks like this:

server {

  passenger_enabled on;
  passenger_app_env staging;

  server_name myapp.com;

  root /home/deploy/myapp/current/public;

  location /blog {
    passenger_enabled off;
    root /home/deploy/myapp/current/public;
    index index.php index.html index.htm;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
   }
}

This worked fine until I thought of moving wordpress directory "blog" from <rails_root>/public/ to /home/deploy/wordpress/blog.

To do that my config was this.

server{
  location ^~ /blog{
    root /home/deploy/wordpress;
    passenger_enabled off;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }

  location / {
    passenger_enabled on;
    root root /home/deploy/myapp/current/public;
    passenger_app_env staging;
  }
}

After this, instead of executing php, nginx started downloading php. Can anyone suggest or point out where I might have gone wrong?

  • 写回答

1条回答 默认 最新

  • doujia2090 2014-11-24 13:06
    关注

    Remove the ^~ operator and it should work. This is because it tells nginx that the following location block prefix must take precedence over regex location blocks (so the php one in particular) during location block election and it tries to serve php files from there (so as a static file and pass it as application/octet-stream because the mime type is likely to be unknown).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?