如何使此嵌套位置配置使用正确的路径来调用PHP程序?

发布于 01-24 13:08 字数 2510 浏览 4 评论 0原文

我已经设置了NGINX,可以允许用户拥有一个用户网站(https://example.org/username映射到/home/home/username/www)。

我想允许我的用户使用PHP。 PHP已安装并在同一服务器上为TLD工作。我不想在此服务器上通常允许PHP,仅用于特定配置。

配置看起来像这样。

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;

  server_name example.org;
  root /var/sites/example.org;

  access_log /var/log/nginx/example_org_access.log main;
  access_log /var/log/nginx/example_org_access.log scripts;
  error_log /var/log/nginx/example_org_error.log info;

  location / {
    try_files maintain.html /index.html @node;
  }

  location ~ ^/(.+?)(/.*)?$ {
    alias /home/$1/www/$2;
    index index.html index.htm;
    autoindex on;

    location ~ ^.+?\.php(/.*)?$ {
      include fastcgi_params;

      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_split_path_info ^(.+\.php)(/.*)$;

      # By default, the variable PATH_INFO is not set under PHP-FPM
      # But some apps need it. If you have a “Bad Request” error, double check this var!
      # NOTE: the separate $path_info variable is required. For more details, see:
      # https://trac.nginx.org/nginx/ticket/321

      set $path_info $fastcgi_path_info;
      fastcgi_param PATH_INFO $path_info;

      fastcgi_param SCRIPT_FILENAME /home$realpath_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }
}

我也有以下日志格式。

log_format scripts 'fastcgi: $realpath_root/$fastcgi_script_name > $request';

输出

fastcgi: -//username/info.php > GET /username/info.php HTTP/2.0

php-fpm日志的输出显示以下内容。

- -  27/Apr/2022:20:47:08 -0600 "GET /username/info.php" 404 - 0.154 2048 0.00%

错误日志将转储以下内容。

2022/04/27 20:47:08 [error] 915687#915687: *14 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 0.0.0.0, server: example.org, request: "GET /username/info.php HTTP/2.0", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", host: "example.org"

所有这些都告诉我,我正在做错了位置错误,特别是fastcgi_param script_filename /home $ realpath_root $ fastcgi_script_name; < /code> line。

将其更改为fastcgi_param script_filename/home/$ 1/www/$ fastcgi_script_namefastcgi_param script_filename/home/home/$ 1/$ 1/www/$ 2不起作用($ 1和$ 2不起作用($ 1和$ 2不起作用)似乎被带入嵌套配置,这很有意义)。

那么,我是否会误以为我可以这样设置这个方式?我该如何完成这项工作?

I have setup nginx to allow users to have a user website (https://example.org/username maps to /home/username/www).

I want to allow my users to use php. Php is installed and working for a tld on the same server. I do not want to allow php generally on this server, just for specific configurations.

The configuration looks like this.

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;

  server_name example.org;
  root /var/sites/example.org;

  access_log /var/log/nginx/example_org_access.log main;
  access_log /var/log/nginx/example_org_access.log scripts;
  error_log /var/log/nginx/example_org_error.log info;

  location / {
    try_files maintain.html /index.html @node;
  }

  location ~ ^/(.+?)(/.*)?$ {
    alias /home/$1/www/$2;
    index index.html index.htm;
    autoindex on;

    location ~ ^.+?\.php(/.*)?$ {
      include fastcgi_params;

      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_split_path_info ^(.+\.php)(/.*)$;

      # By default, the variable PATH_INFO is not set under PHP-FPM
      # But some apps need it. If you have a “Bad Request” error, double check this var!
      # NOTE: the separate $path_info variable is required. For more details, see:
      # https://trac.nginx.org/nginx/ticket/321

      set $path_info $fastcgi_path_info;
      fastcgi_param PATH_INFO $path_info;

      fastcgi_param SCRIPT_FILENAME /home$realpath_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }
}

I also have the following log format.

log_format scripts 'fastcgi: $realpath_root/$fastcgi_script_name > $request';

which outputs

fastcgi: -//username/info.php > GET /username/info.php HTTP/2.0

The php-fpm log shows the following for the same request.

- -  27/Apr/2022:20:47:08 -0600 "GET /username/info.php" 404 - 0.154 2048 0.00%

And the error log dumps the following.

2022/04/27 20:47:08 [error] 915687#915687: *14 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 0.0.0.0, server: example.org, request: "GET /username/info.php HTTP/2.0", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", host: "example.org"

All this tells me I'm doing the nested location wrong, specifically the fastcgi_param SCRIPT_FILENAME /home$realpath_root$fastcgi_script_name; line.

Changing that to fastcgi_param SCRIPT_FILENAME /home/$1/www/$fastcgi_script_name or fastcgi_param SCRIPT_FILENAME /home/$1/www/$2 doesn't work ($1 and $2 don't seem to be carried into the nested configuration, which makes sense).

So, am I mistaken in the belief I can set this up this way? How can I make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丘比特射中我2025-01-31 13:08:05

@Richard Smith的评论使我指出了正确的方向。此配置有效。

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;

  server_name example.com;
  root /var/sites/example.com;

  access_log /var/log/nginx/example_com_access.log main;
  access_log /var/log/nginx/example_com_access.log scripts;
  error_log /var/log/nginx/example_com_error.log info;

  try_files index.html index.htm /index.html =404;

  location ~ ^/(?<username>[^/]+?)(?<userpath>/.*)?$ {
    alias /home/$username/www$userpath;

    location ~ ^.+?\.php(?:/.*)?$ {
      include fastcgi_params;

      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_split_path_info ^(.+\.php)(/.*)$;

      # By default, the variable PATH_INFO is not set under PHP-FPM
      # But some apps need it. If you have a “Bad Request” error, double check this var!
      # NOTE: the separate $path_info variable is required. For more details, see:
      # https://trac.nginx.org/nginx/ticket/321

      set $path_info $fastcgi_path_info;
      fastcgi_param PATH_INFO $path_info;

      fastcgi_param SCRIPT_FILENAME $request_filename;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }
}

@Richard Smith's comment helped point me in the right direction. This configuration works.

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;

  server_name example.com;
  root /var/sites/example.com;

  access_log /var/log/nginx/example_com_access.log main;
  access_log /var/log/nginx/example_com_access.log scripts;
  error_log /var/log/nginx/example_com_error.log info;

  try_files index.html index.htm /index.html =404;

  location ~ ^/(?<username>[^/]+?)(?<userpath>/.*)?$ {
    alias /home/$username/www$userpath;

    location ~ ^.+?\.php(?:/.*)?$ {
      include fastcgi_params;

      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_split_path_info ^(.+\.php)(/.*)$;

      # By default, the variable PATH_INFO is not set under PHP-FPM
      # But some apps need it. If you have a “Bad Request” error, double check this var!
      # NOTE: the separate $path_info variable is required. For more details, see:
      # https://trac.nginx.org/nginx/ticket/321

      set $path_info $fastcgi_path_info;
      fastcgi_param PATH_INFO $path_info;

      fastcgi_param SCRIPT_FILENAME $request_filename;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文