Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx configuration with multiple location blocks

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 which resides in /home/hamed/laravel in which its public directory should be served.
  2. I have a Wordpress installation in /home/hamed/www/blog.

And this is my nginx configuration:

server {
        listen  443 ssl;
        server_name example.com www.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 {
                root /home/hamed/www/blog;
                try_files $uri $uri/ /blog/index.php?do=$request_uri;
        }

        location / {
                root /home/hamed/laravel/public;
                try_files $uri $uri/ /index.php?$request_uri;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        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;
        }

}

The problem is when trying to access the wordpress section by calling example.com/blog still the the laravel installtion takes over the request.

Now I have tried replacing root directive inside location blocks with alias to no avail.

According to this guide having the index directive or try_files inside location triggers an internal redirect which I suspect causes this behavior.

Would someone please help me figure this out?

like image 763
2hamed Avatar asked Dec 04 '15 07:12

2hamed


People also ask

What are the nginx location blocks for?

The location directive within NGINX server block allows to route request to correct location within the file system. The directive is used to tell NGINX where to look for a resource by including files and folders while matching a location block against an URL.

Can nginx host multiple sites?

Multiple domains can be hosted on Nginx using server blocks. If you choose a VPS as your hosting platform, all your websites can reside on a single server, giving you the flexibility to take control of things yourself. This guide explains hosting multiple websites on a single server using Nginx. Nginx is a web server.

How do I get nginx to listen on multiple ports?

To make Nginx Listen on multiple ports for a single virtual host file, you can add multiple listen directives. If you want to make Nginx listen for different virtual hosts on different ports, you can use different ports in listen directive in different virtual host files. It's that easy!


1 Answers

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.

like image 180
Richard Smith Avatar answered Sep 20 '22 14:09

Richard Smith