【问题标题】:FastCGI - Rewrite - Location with NginxFastCGI - 重写 - 使用 Nginx 定位
【发布时间】:2012-11-27 03:48:38
【问题描述】:

我刚切换到 nginx,但我的 URL 重写遇到了问题

我用过

location /id/ {
       rewrite ^/id/(.*) /index.php?id=$1 break;
}

但是 php 代码没有被解释,最糟糕的是它是原始下载的。 然而 .php 文件的配置如下:

    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/my_app$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
    }

我的虚拟主机出了什么问题?

编辑:这是整个虚拟主机 服务器{

    listen   80; ## listen for ipv4
    server_name  viditx.com www.viditx.com; ## change this to your own domain name

   # I find it really useful for each domain & subdomain to have
   # its own error and access log
    error_log /var/log/nginx/viditx.com.error.log;
    access_log  /var/log/nginx/viditx.com.access.log;
root /var/www/viditx;   

location / {
        # Change this to the folder where you want to store your website
        index  index.html index.htm index.php;
}
location /phpmyadmin {
           root /usr/share/;
           index index.php index.html index.htm;
           location ~ ^/phpmyadmin/(.+\.php)$ {
                   try_files $uri =404;
                   root /usr/share/;
                   fastcgi_pass 127.0.0.1:9000;
                   fastcgi_index index.php;
                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                   include /etc/nginx/fastcgi_params;
           }
           location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                   root /usr/share/;
           }
    }
    location /phpMyAdmin {
           rewrite ^/* /phpmyadmin last;
    }


    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;

    location = /50x.html {
            root   /var/www/nginx-default;
    }

    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            # again, change the directory here to your website's root directory
            # make sure to leave $fastcgi_script_name; on the end!
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
    }

}

【问题讨论】:

    标签: .htaccess url-rewriting nginx rewrite


    【解决方案1】:

    Did you read the documentation?

    break -> last

    location /id/ {
        rewrite ^/id/(.*)$ /index.php?id=$1 last;
    }
    

    【讨论】:

    • 谢谢你,先生,我想我没有任何借口,即使我读了它。
    【解决方案2】:

    你应该试试:

    location /id/ {
           rewrite '^/id/(.*)$' /index.php?id=$1 break;
    }
    

    和:

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include     /etc/nginx/fastcgi_params;
    }
    

    玩得开心!

    【讨论】:

    • 不起作用。每当我访问 mywebsite.com/id/abc 时,仍会下载 php 文件。如果这有任何帮助,我按照本教程进行操作 blog.meltingice.net/server-management/…
    • 请给我们您的 nginx.conf 文件
    • 用整个虚拟主机(没有任何重写)+ conf 编辑了我的问题
    猜你喜欢
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 2011-11-24
    • 2014-12-06
    相关资源
    最近更新 更多