【问题标题】:nginx: Map single static URL to a PHP filenginx:将单个静态 URL 映射到 PHP 文件
【发布时间】:2012-12-31 11:11:25
【问题描述】:

我已经安装好 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 并在 location 块的根指令中查找它。从错误日志:

2013/01/16 15:30:42 [错误] 20533#0: *4 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”

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

【问题讨论】:

  • 只是一个想法:include fastcgi_params; 不是覆盖您的fastcgi_param SCRIPT_FILENAME /opt/www/custom_scripts/signup-processor.php; 吗?你能试着在你的“root”指令之后移动它(include fastcgi_params;)吗?
  • 是的,就是这样。 fastcgi_params 文件中有一行是由部门中的某个人添加的。非常感谢朱利安。

标签: nginx fastcgi


【解决方案1】:

末尾的include 指令:

    include         fastcgi_params;

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

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

【讨论】:

  • 感谢您的回答!后续问题:fastcgi_params 最初填充在哪里?
  • 在nginx安装目录下,视版本而定,一般在/etc/nginx/fastcgi_params
  • @Julien,你能回答stackoverflow.com/questions/31332494/…请吗?
【解决方案2】:

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

不只是

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

更清洁并做同样的事情?

【讨论】:

    猜你喜欢
    • 2019-06-22
    • 2015-03-24
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多