0

I'm trying to configure nginx to use two different document root depending on url, so if some one connects to www.example.com will get site from /portal/public_html but if client connect to www.example.com/account will get sites from other directory /portal2/public and that site should be served by php 7. I'm using variables in nginx configuration to achieve my goal, but something is not working as I thought it should. I'm getting errors 500. In my logs I see that nginx is looking for site files in his default directory "/usr/share/nginx/html" 2017/11/20 10:31:38 [debug] 21729#0: *421789 fastcgi param: "DOCUMENT_ROOT: /usr/share/nginx/html

Maybe the configuration will be simpler to read and understand then my rusty english so this is my nginx configuration:

server {
    listen 81 default;
    server_name www.example.com;

    access_log /var/log/nginx/www.example.com.access.log;
    error_log /var/log/nginx/www.example.com.error.log debug;

    location / {

    set $my_root  /portal/public_html;

    #PHP v5
        set $php_host_port 127.0.0.1:9000;
        root /$my_root;
        index  index.php;
        proxy_read_timeout 200;

        include /nginx_conf/portal_rewrite_params; 

    }

    location ~^/(account|new-cart|signin|register|n) {
    set $my_root  /portal2/public;
        root   $my_root;
        proxy_read_timeout 200;
        index  index.php;
        try_files $uri $uri/ /index.php$is_args$args;

    #PHP v7  
     set $php_host_port 127.0.0.1:9071;

    }

    location ~ \.php$ {

        fastcgi_pass   $php_host_port;
        fastcgi_index  index.php; 
        include /nginx_conf/fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  DOCUMENT_ROOT $document_root;
    }
}

So my question how to achieve that nginx serve same site from different document root and using different php depending on URL? Is my approach good ?

My enviroment is centos 7, nginx/1.10.2, php 5.6 and php7.1

B14D3
  • 5,188
  • 15
  • 64
  • 83
  • You can't use variables like that. Use nested locations - like [this answer](https://serverfault.com/questions/876332/nginx-php-fpm-uri-alias-and-multiple-php-directories/876349#876349). – Richard Smith Nov 20 '17 at 10:14

1 Answers1

2

Your variables are confusing..

 root /portal/public_html; 

 location ~^/(account|new-cart|signin|register|n) {
     root /portal2/public;
     index index.php;
     location ~* \.php {
         fastcgi_index  index.php; 
         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_param  DOCUMENT_ROOT $document_root;
         fastcgi_pass 127.0.0.1:9071;
         # include other fascgi_params
     }
 }

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

 location ~* \.php {
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_index  index.php; 
     fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
     fastcgi_param  DOCUMENT_ROOT $document_root;
     # include other fascgi_params
 }
  • I was just typing a very similar solution. :) But I suggest using PHP-FPM and sock files instead IP ports. With that you can avoid a php-pool attack. – Bert Nov 20 '17 at 10:21
  • 1
    sock vs ip is another huge topic :)))) – Alexander Makarenko Nov 20 '17 at 10:21
  • This doesn't work, it looks like nothing is going to first location block and everything is redirected to php 5 so to second location block – B14D3 Nov 22 '17 at 08:11
  • @B14D3 Is your problem unsolved? Is your case absolutly the same as in the question? If not, could you provide your config please, I'd like to figure out. – Alexander Makarenko Nov 23 '17 at 09:41