【问题标题】:Ngnix rewrite PHP files get downloadedNginx 重写 PHP 文件被下载
【发布时间】:2015-09-12 13:15:12
【问题描述】:

是的,我知道这个问题已经被问过了,不幸的是,即使我尝试了各种答案,但似乎没有一个适用于我的情况。

基本上,我正在尝试使用 Ngnix 进行 mod 重写,但出于某种原因,在某些页面中它会将您带到正确的页面,而在其他页面上它只会下载文件。

这是我的默认站点配置文件:

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 成功到达 /account.php),而有些只是下载源代码。

是的,所有文件都存在。

什么可能导致这个问题?

【问题讨论】:

    标签: php nginx config


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 2013-01-12
      • 1970-01-01
      相关资源
      最近更新 更多