Ngnix重写PHP文件得到下载

是的,我知道这个问题已经被问及,不幸的是,即使我已经尝试了各种答案,他们似乎都没有在我的情况下工作。

基本上,我试图用Ngnix做一个mod重写,但由于某些原因,在一些页面中,它会带你到正确的页面,而在其他页面上,它只会下载文件。

这是我的默认站点configuration文件:

server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.php index.html index.htm; # Make site accessible from http://localhost/ server_name studiowolfree.com; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests #location /RequestDenied { # proxy_pass http://127.0.0.1:8080; #} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #error_page 500 502 503 504 /50x.html; #location = /50x.html { # root /usr/share/nginx/html; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} # nginx configuration error_page 500 /error.php?id=500; error_page 404 /error.php?id=404; error_page 403 /error.php?id=403; autoindex off; location /articles { rewrite ^(.*)$ /articles.php last; rewrite ^(.*)$ /articles.php last; } location /workshops { rewrite ^(.*)$ /workshops.php last; rewrite ^(.*)$ /workshops.php last; } location /account { rewrite ^(.*)$ /account.php last; rewrite ^(.*)$ /account.php last; } location /article { rewrite ^/article/([^/]*)$ /article.php?shortname=$1 last; rewrite ^/article/([^/]*)/$ /article.php?shortname=$1 last; } location /category { rewrite ^/category/([^/]*)$ /category.php?shortname=$1 last; rewrite ^/category/([^/]*)/$ /category.php?shortname=$1 last; } location /users { rewrite ^/users/([^/]*)$ /users.php?id=$1 last; rewrite ^/users/([^/]*)/$ /users.php?id=$1 last; } location /workshop { rewrite ^/workshop/([^/]*)$ /workshop.php?id=$1 last; rewrite ^/workshop/([^/]*)/$ /workshop.php?id=$1 last; rewrite ^/workshop/([^/]*)$ /workshop.php?id=$1 last; rewrite ^/workshop/([^/]*)/$ /workshop.php?id=$1 last; rewrite ^/workshop/([^/]*)/item/([^/]*)$ /workshop.php?id=$1&step=$2 last; rewrite ^/workshop/([^/]*)/$ /workshop.php?id=$1 last; rewrite ^/workshop/([^/]*)$ /workshop.php?id=$1 last; rewrite ^/workshop/([^/]*)/item/([^/]*)/$ /workshop.php?id=$1&step=$2 last; } location /.htaccess { deny all; } } 

奇怪的是,一些URL到他们各自的文件(/帐户,例如成功达到/account.php),一些只是下载源代码。

是的,所有文件都存在。

什么可能导致这个问题?

确保首先所有文件都由nginx拥有,并且将运行php-fpm的用户更改为nginx。 这将允许这两个处理文件。

也可以尝试把它放在你的nginx配置文件中。

 location / { try_files $uri $uri/ /index.php?q=$request_uri; } 

代替

 location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules }