php - Nginx 重写 PHP 文件获取下载

标签 php nginx config

是的,我知道这个问题已经被问过了,不幸的是,尽管我尝试了各种答案,但似乎没有一个对我的情况有效。

基本上,我正在尝试使用 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),而有些只是下载源代码。

是的,所有文件都存在。

可能导致此问题的原因是什么?

最佳答案

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

关于php - Nginx 重写 PHP 文件获取下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31058687/

相关文章:

使用 TortoiseGit 和 Ansible 执行 Git Pull/Clone 时出现 GitLab SSL 握手错误

ssl - 使用 nginx 将通配符子域重定向到不同的顶级域

ruby-on-rails - 为什么在本地强制使用 https?

php - 动态命名输入类型

javascript - 以更低的成本找到最佳组合

javascript - 如何获取扩展图像文件 jquery.filer

php - 尝试使用在 NGINX/HHVM 上运行的 Symfony 设置 Memcached

android - Phonegap 本地构建 - jquery ajax 错误 : readystate 0 responsetext status 0 statustext error

php - Laravel 5 配置文件中的自定义配置文件访问

php - 使用 id 数组更新和删除多行