大网数据广告活动
服务器运维问答

nginx multiple (for now two) php projects via php-fpm in subdirectories using alias

· 52CE 我爱测

For the love of nginx, I can't wrap my head around this issue. Desired: I want two simple php projects (wordpress in the long haul) in two sub locations under one server block. Side note: These projects reside in two different di…

问题原文

原帖链接 · mahatmanich · CC BY-SA 3.0

For the love of nginx, I can't wrap my head around this issue.

Desired: I want two simple php projects (wordpress in the long haul) in two sub locations under one server block. Side note: These projects reside in two different directories on the server deployed with capistrano.

Problem: I either end up with a 404, 403 or a direct octet stream download of the index.php. On the latter I seem to hit the correct index.php but it is not passed to the php-fpm block. php-fpm is working and not the issue (tested in other serverblock without sublocations)

I have looked all over the web and have tried gazillions of "working" configs, but its not coming together.

Plan: Below you see a working nginx vhost, hitting the right index.html files in the correct alias directories. Thus I am halfway there.

With your help I would like to adapt the config below, to change the index to index.php and get php working on location /staging and /production.

In the location /production you see one config (commented out) how I tried to get php working.

server {
  listen 82;
  listen [::]:82;

  server_name nginx-web.ch;

  access_log /var/log/nginx/nginx-web_access.log;
  error_log /var/log/nginx/nginx-web_error.log;

  location  /staging {
    alias /var/www/nginx-web1/current;
    index index.html
    add_header X-debug-message "Location web1";
  }

  location /production {
    alias /var/www/nginx-web/current;
    index index.html
    add_header X-debug-message "Location web";

    #try_files $uri $uri/ /production/index.php;

    #location ~ \.php$ {
      # add_header X-debug-message "Location ~ php";
      # try_files $uri =404;
      # fastcgi_split_path_info ^(.+\.php)(/.+)$;
      # fastcgi_pass unix:/var/run/php5-fpm.sock;
      # fastcgi_index index.php;
      # include fastcgi_params;
      # fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #}
  }
}

Here is a working server block it tried to adapt for sublocations, without success :(

server {
  listen 80;
  listen [::]:80;

  server_name testdev;

  access_log /var/log/nginx/wp_access.log;
  error_log  /var/log/nginx/wp_error.log;

  root /var/www;
  index index.php;

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

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}

UPDATE with WORKING CONFIG (gotta <3 serverfault/stackoverflow):

Here is the final working configuration, many thanks to @RichardSmith

server {
    listen 82;
    listen [::]:82;

    server_name nginx-web.ch;

    access_log /var/log/nginx/nginx-web_access.log;
    error_log /var/log/nginx/nginx-web_error.log;

    index index.php;

    location ^~ /staging/ {
      alias /var/www/nginx-web1/current/;

      if (!-e $request_filename) { rewrite ^ /staging/index.php last; }

      location ~ \.php$ {
       if (!-f $request_filename) { return 404; }

       include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME $request_filename;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
       }
    }

    location /production {
      alias /var/www/nginx-web/current;

      if (!-e $request_filename) { rewrite ^ /production/index.php last; }

      location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME $request_filename;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }
}

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

This pattern works:

location ^~ /prefix/ {
    alias /path/to/root/;
    if (!-e $request_filename) { rewrite ^ /prefix/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        fastcgi_pass   ...;
    }
}

Use the ^~ prefix to avoid other regular expression location blocks taking precedence. See this document.

The value of the location and the alias both end with / or neither end with /. See this document.

Avoid using alias and try_files together due to this issue and see this caution on the use of if.

Use $request_filename as the computed value of SCRIPT_FILENAME (as it works with both alias and root).

Always set fastcgi_param after including the fastcgi_params file, to avoid the latter silently overwriting the local value.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-11-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。