nginx configuration with multiple location blocks

后端 未结 2 726
走了就别回头了
走了就别回头了 2020-12-10 07:42

I\'m trying to configure nginx to serve 2 different php scripts from 2 different location. The configuration is as follows.

  1. I have a Laravel installation whi
相关标签:
2条回答
  • 2020-12-10 08:03

    Thanks to @RichardSmith I finally managed to create the right configuration. Here is the final working config. I had to use the combination of nested location blocks and an inverse regex match for it to work.

    server {
        listen  443 ssl;
            server_name example.com;
            root /home/hamed/laravel/public;
    
    #        index index.html index.htm index.php;
    
            ssl_certificate /root/hamed/ssl.crt;
            ssl_certificate_key /root/hamed/ssl.key;
    
            location ~ ^/blog(.*)$ {
            index index.php;
            root /home/hamed/www/;
            try_files $uri $uri/ /blog/index.php?do=$request_uri;
    
            location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        #fastcgi_pass 127.0.0.1:9000;
                        fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include fastcgi_params;
                }
    
            }
    
        location ~ ^((?!\/blog).)*$ { #this regex is to match anything but `/blog`
            index index.php;
                    root /home/hamed/laravel/public;
                    try_files $uri $uri/ /index.php?$request_uri;
            location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        #fastcgi_pass 127.0.0.1:9000;
                        fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include fastcgi_params;
                }
            }
    
    
            error_page 404 /404.html;
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                    root /usr/share/nginx/html;
            }
    
    
    }
    
    0 讨论(0)
  • 2020-12-10 08:20

    The problem is that location ~ \.php$ { ... } is responsible for handling all of your php scripts, which are divided across two different roots.

    One approach is to use a common root for the server container and perform internal rewrites within each prefix location block. Something like:

    location /blog {
      rewrite ^(.*\.php)$ /www$1 last;
      ...
    }
    location / {
      rewrite ^(.*\.php)$ /laravel/public$1 last;
      ...
    }
    location ~ \.php$ {
      internal;
      root /home/hamed;
      ...
    }
    

    The above should work (but I have not tested it with your scenario).

    The second approach is to use nested location blocks. The location ~ \.php$ { ... } block is then replicated in each application's location block. Something like:

    location /blog {
      root /home/hamed/www;
      ...
      location ~ \.php$ {
        ...
      }
    }
    location / {
      root /home/hamed/laravel/public;
      ...
      location ~ \.php$ {
        ...
      }
    }
    

    Now that one has been tested to work.

    0 讨论(0)
提交回复
热议问题