Ngnix rewrite PHP files get downloaded

时间:2015-06-25 19:02:47

标签: php nginx config

yes, I know that this question has already been asked and unfortunately, even though I've tried various answers, none of them seem to work on my case. Basically, I'm trying to do a mod rewrite with Ngnix, but for some reason, in some pages it'll take you to the correct page while on other pages it'll just download the file. This is my default site config file: 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; } } The weird is that some URLs go to their respective files (/account for example successfully reaches /account.php) and some just download the source code. Yes, all files exist. What could be causing this issue?

1 个答案:

答案 0 :(得分:0)

确保首先所有文件都归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
}
相关问题