【问题标题】:nginx, fastcgi and how to rewrite a URLnginx、fastcgi 以及如何重写 URL
【发布时间】:2012-01-24 21:34:24
【问题描述】:

我在 nginx x 中使用 fastcgi 进行了非常简单的重写。

假设我的域是 www.test.com/test.fcgi

如何重写到 www.test.com?如果我去 www.test.com/test.fcgi 它工作。

server {
    listen   80;
    server_name 127.0.0.1;


    location ~ \.fcgi$ {
                    rewrite ^/test.fcgi/(.*)$ $1 last;
        root   /var/www;
                    include /etc/nginx/fastcgi_params;
                    #fastcgi_pass   unix:/tmp/nginx.socket;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.html;
        fastcgi_param  SCRIPT_FILENAME  /$fastcgi_script_name;
        include fastcgi_params;
    }

}

【问题讨论】:

    标签: url-rewriting nginx fastcgi


    【解决方案1】:

    好吧,你真的不会在 Nginx 中使用这样的 .fcgi 文件,所以很难回答所问的问题。 Nginx 与 FasCGI 一起工作的方式与与其他上游服务器的工作方式相同,通过将请求传递给 Unix 或 TCP 套接字。

    您将使用重写请求的标准方法之一重写请求。在您的位置部分使用如下内容:

    rewrite ^/test.fcgi/(.*)$ $1 last;
    

    然后,您将请求传递给监听 FastFGI 请求的守护进程,如下所示:

    fastcgi_pass localhost:8001;
    fastcgi_index index.fcgi;
    

    根据后端流程和设置的具体情况,您可能需要其他选项,如果您向我们提供更多信息,我们可能会提供进一步的帮助。

    有关这两个模块的文档和示例,请参阅 (http://wiki.nginx.org/HttpFcgiModule)[此处] 和 (

    【讨论】:

    • 服务器 { 听 80;服务器名称 127.0.0.1; location ~ \.fcgi$ { rewrite ^/test.fcgi/(.*)$ $1 last;根 /var/www;包括/etc/nginx/fastcgi_params; #fastcgi_pass unix:/tmp/nginx.socket; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.html; fastcgi_param SCRIPT_FILENAME /$fastcgi_script_name;包括 fastcgi_params; } }
    • 嗯....我想重写。要运行该页面,我必须转到 127.0.0.1/test.fcgi 但如果我转到 127.0.0.1 我会找不到页面。我把索引改成了fastcgi_index test.fcgi;
    • 总之...我只想去 127.0.0.1 运行脚本,但我必须去 127.0.0.1/test.fcgi
    • 您将这些请求传递给什么语言或框架? Nginx 不会自己生成 Cgi 或 Fcgi 处理程序,它希望它们已经在监听。 rwrite 应该删除该路径,但我不确定它是否会自行解决问题。
    • 我在 ubunto 上使用 nginx。我使用 spawn-fcgi -u root -g root -G www-data -a 127.0.0.1 -p 9000 -f /var/www/test.fcgi 来生成。
    【解决方案2】:

    将此添加到您的 nginx 配置中,相信它会对您有所帮助。

    location = / {
            root /var/www/
            include /etc/nginx/fastcgi_params;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.html;
            fastcgi_param  SCRIPT_FILENAME  /$fastcgi_script_name;
            include fastcgi_params;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 2019-01-24
      • 2015-12-20
      • 1970-01-01
      • 2011-05-18
      • 2019-11-16
      相关资源
      最近更新 更多