Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A same "Location" rule for multi "Server" block

Tags:

nginx

I have to configure multi https website with a dedicated certificate for each website. It works fine like that.

server {
        listen   443;
        server_name client1.localhost.eu;

        ssl on;
        ssl_certificate ...;
        ssl_certificate_key ...;

        root   /var/www/client1;

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

server {
        listen   443;
        server_name client2.localhost.eu;

        ssl on;
        ssl_certificate ...;
        ssl_certificate_key ...;

        root   /var/www/client2;

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

Now, I would like to factorize the "location" block, because it is always the same. Is it possible ? (I have also tried to have only on server block, but it's not possible to put a variable in the ssl attribute)

Thanks a lot for your help.

Eric

like image 692
elhostis Avatar asked Mar 03 '15 07:03

elhostis


People also ask

What is a location block?

A location block lives within a server block and is used to define how Nginx should handle requests for different resources and URIs for the parent server. The URI space can be subdivided in whatever way the administrator likes using these blocks. It is an extremely flexible model.

Can we have 2 Nginx on the same server?

Yes, its technically possible to install 2 nginx instances on the same server but I would do it another way. 1 - You could just create multiple EC2 instances. The downside of this approach is that maybe it's gets harder to maintain depending on how many instances you want.

What is server_name _ in Nginx?

As a convention, the underscore is used as a server name for default servers. From http://nginx.org/en/docs/http/server_names.html. In catch-all server examples the strange name “_” can be seen: server { listen 80 default_server; server_name _; return 444; }

How does location work in Nginx?

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.


1 Answers

Use include directive for such factorization:

include

Create file in the nginx config folder like /etc/nginx/conf.d/location_php.cnf (not .conf to avoid auto-loading by nginx)

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

and then include it into server blocks:

server {
        listen   443;
        server_name client1.localhost.eu;

        ssl on;
        ssl_certificate ...;
        ssl_certificate_key ...;

        root   /var/www/client1;
        include /etc/nginx/conf.d/location_php.cnf;
        # OR use relative path to nginx config root:
        # include conf.d/location_php.cnf;
}
like image 62
Aleksey Deryagin Avatar answered Sep 29 '22 02:09

Aleksey Deryagin