Nginx下载PHP而不是渲染

我试图从Apache移动一个Wordpress网站到Nginx。 当我到主站点时,它按预期呈现。 当我点击一个post,它试图下载index.php而不是处理/渲染它。

我按照https://www.linode.com/docs/websites/lemp/lemp-server-on-ubuntu-16-04设置nginx

我的nginx.conf

 user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_disable "msie6"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 

和我的li394-200.members.linode.com文件在sites-available

 server { listen 80; listen [::]:80; server_name li394-200.members.linode.com; root /var/www/html/li394-200.members.linode.com/public_html; index index.html index.php; location / { try_files $uri $uri/ /index.php$args =404; } location ~ .*\.php$ { include snippets/fastcgi-php.conf; include fastcgi_params; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_param SCRIPT_FILENAME /var/www/html/li394-200.members.linode.com/public_html$fastcgi_script_name; } } 

如果我导航到http://li394-200.members.linode.com/phptest.php它按预期呈现。 另外http://li394-200.members.linode.com/index.php是正确的。 但是,如果我去http://li394-200.members.linode.com/archives/2016/11/02/international-keyboard-shortcut-day-2016/它说'你已经select打开…申请/八位字节stream“。 当我说OK并下载它时,它是我的wordpress目录中的index.php。

我的永久链接结构是/archives/%year%/%monthnum%/%day%/%postname%/ 。 如果我将“永久链接”更改为“纯文本”,则可以正确导航到http://li394-200.members.linode.com/?p=11240 。

我遵循http://nginxlibrary.com/wordpress-permalinks/的build议,并添加

 try_files $uri $uri/ /index.php?args; 

到位置块。 当我重新启动nginx时,我得到了

 [emerg] try_files directive is duplicate in /etc/nginx/snippets/fastcgi-php.conf:5 

我什至不知道我在哪里,包括该文件。 但是那个文件看起来像

 # regex to split $uri to $fastcgi_script_name and $fastcgi_path fastcgi_split_path_info ^(.+\.php)(/.+)$; # Check that the PHP script exists before passing it try_files $fastcgi_script_name =404; # Bypass the fact that try_files resets $fastcgi_path_info # see: http://trac.nginx.org/nginx/ticket/321 set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info; fastcgi_index index.php; include fastcgi.conf; 

所以$fastcgi_script_name必须包含相同的东西,但我不知道如何看看是什么。 我在我的public_html目录中看不到任何看起来像fast-cgi脚本的东西(尽pipe我看到它时可能不知道)。

无论我使用我的自定义永久链接还是默认链接,URI中都没有.php ,所以我不知道该位置指令是如何捕获它的。

有两点想法:

  • 你声明default_type application/octet-stream; 。 请将其设置为default_type text/plain; 或者干脆删除它,因为我认为你不想为“多用途应用程序文件”服务,更正? 更多信息在这里: http : //www.mime-type.net/application/octet-stream/
  • 请检查您的php-fpm套接字是否设置正确。 它可能是/ var / run / …或者命名不同,如果你有一个套接字运行(检查你的php-fpm设置); 否则大多数安装使用环回端口9000。

编辑:

刚刚意识到你的configuration另一个问题:你应该改变“/index.php$args”或“/index.php?args”为“/index.php?arargs”。

原因如下:nginx匹配你的正则expression式,然后将variables“$ args”设置为斜线后的任何值。 问号又将文件名与参数分开。

所以“/index.php$args”将会以“/index.phpwhateverargument”结尾,而“/index.php?args”只会保留“/index.php?args”。 你想要的是“/index.php?whateverargument”。