nginx configuration with multiple location blocks

后端 未结 2 727
走了就别回头了
走了就别回头了 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: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.

提交回复
热议问题