nginx:将单个静态URL映射到PHP文件 [英] nginx: Map single static URL to a PHP file

查看:472
本文介绍了nginx:将单个静态URL映射到PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WordPress installation up and running without issue. There is an element on the page that is not part of WP, but our own custom PHP script. It's just there to handle form POST requests and process it in someway.

有一个要求,我们没有以.php结尾的扩展名.我的表单正在提交类似的内容:

There is a requirement that we don't have extensions that end in .php. My form is submitting to something like:

/places/signup.html

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

In my nginx config, I have this:

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,并在location块的root指令中寻找它.从错误日志中:

The problem is: fastcgi or nginx (I have no idea which) is taking the literal URL and looking for it in the location block's root directive. From the error log:

2013/01/16 15:30:42 [错误] 20533#0:* 4在stderr中发送的FastCGI: 无法打开主脚本: /opt/www/custom_scripts/places/signup.html (无此文件或 目录)",同时从上游读取响应标头,客户端: 66.172.31.153,服务器:domain.com,请求:"POST/places/signup.html HTTP/1.1",上游:"fastcgi://unix:/tmp/php5-fpm.sock:",主机: "www.domain.com"

2013/01/16 15:30:42 [error] 20533#0: *4 FastCGI sent in stderr: "Unable to open primary script: /opt/www/custom_scripts/places/signup.html (No such file or directory)" while reading response header from upstream, client: 66.172.31.153, server: domain.com, request: "POST /places/signup.html HTTP/1.1", upstream: "fastcgi://unix:/tmp/php5-fpm.sock:", host: "www.domain.com"

我认为"fastcgi_param SCRIPT_FILENAME/opt/www/custom_scripts/signup-processor.php;"会迫使它使用该特定脚本?我想不是吗?

I thought "fastcgi_param SCRIPT_FILENAME /opt/www/custom_scripts/signup-processor.php;" would force it to use that specific script? I guess not?

推荐答案

最后的include指令:

    include         fastcgi_params;

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

probably has another fastcgi_param directive with a SCRIPT_FILENAME parameter :

    fastcgi_param   SCRIPT_FILENAME  /another/script/file/name;

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

As it's at the end of the file, it will override your value.

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

If you want to keep some settings from your fastcgi_params file, but override them with specific ones, you should place the include directive at the beginning :

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;
}

这篇关于nginx:将单个静态URL映射到PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆