nginx:将单个静态URL映射到PHP文件

时间:2013-01-17 00:08:34

标签: nginx fastcgi

我有一个WordPress安装启动并运行没有问题。页面上有一个元素不是WP的一部分,而是我们自己的自定义PHP脚本。只是在那里处理表单POST请求并以某种方式处理它。

要求我们没有以.php结尾的扩展名。我的表单提交的内容如下:

/places/signup.html

在我的nginx配置中,我有这个:

server {
    listen       87948; # It's behind a reverse proxy
    server_name domain.com www.domain.com;

    include /etc/nginx/proxy_params;

    index index.php;
    root /opt/www/public_html;

    location /favicon.ico {
        return 404;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location ~ \.php$ {
        add_header Access-Control-Allow-Origin "*";
        client_body_buffer_size 4096k;
        fastcgi_pass unix:/tmp/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /opt/www/public_html/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    # LOOK HERE
    location ~ /places/signup.html {
        root                             /opt/www/custom_scripts/;
        fastcgi_pass    unix:/tmp/php5-fpm.sock;
        fastcgi_param   SCRIPT_FILENAME  /opt/www/custom_scripts/signup-processor.php;
        fastcgi_param   QUERY_STRING     $query_string;
        fastcgi_param   REQUEST_METHOD   $request_method;
        fastcgi_param   CONTENT_TYPE     $content_type;
        fastcgi_param   CONTENT_LENGTH   $content_length;
        include         fastcgi_params;
    }

}

问题是:fastcgi或nginx(我不知道哪个)正在使用文字URL并在位置块的root指令中查找它。从错误日志中:

  

2013/01/16 15:30:42 [错误] 20533#0:* 4在stderr发送的FastCGI:   "无法打开主脚本:    /opt/www/custom_scripts/places/signup.html (没有此类文件或   目录)"从上游读取响应头,客户端:   66.172.31.153,server:domain.com,request:" POST /places/signup.html HTTP / 1.1",upstream:" fastcgi:// unix:/ tmp / php5-fpm。袜子:",主持人:   " www.domain.com"

我想" fastcgi_param SCRIPT_FILENAME /opt/www/custom_scripts/signup-processor.php;"会强迫它使用那个特定的脚本吗?我猜不是吗?

2 个答案:

答案 0 :(得分:7)

最后的include指令:

    include         fastcgi_params;

可能有另一个带有fastcgi_param参数的SCRIPT_FILENAME指令:

    fastcgi_param   SCRIPT_FILENAME  /another/script/file/name;

由于它位于文件的末尾,它将覆盖您的值。

如果您想保留fastcgi_params文件中的某些设置,但是使用特定的设置覆盖它们,则应将include指令放在开头:

location ~ /places/signup.html {
    include         fastcgi_params;
    root                             /opt/www/custom_scripts/;
    fastcgi_pass    unix:/tmp/php5-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME  /opt/www/custom_scripts/signup-processor.php;
    fastcgi_param   QUERY_STRING     $query_string;
    fastcgi_param   REQUEST_METHOD   $request_method;
    fastcgi_param   CONTENT_TYPE     $content_type;
    fastcgi_param   CONTENT_LENGTH   $content_length;
}

答案 1 :(得分:0)

看起来你已经有了处理PHP文件的指令。

不仅仅是

location ~ /places/signup.html {
   root /opt/www/custom_scripts;
   try_files $uri /signup-processor.php;
}

更干净并做同样的事情吗?