Using variable in vhost

Karl Johnson at
Tue Jun 12 19:48:19 UTC 2018


I have a nginx multi-user setup that use the same fpm config for all vhost
but each vhost has his own user so I had to set a variable in the vhost
config to set the fastcgi_pass path in the included file. This way the
vhost config is always clean.

I've read somewhere that variable in vhost is not recommended. What do you
think of this setup? It's currently working pretty well so I was wondering.


[root at web ~]# cat /etc/nginx/conf.d/vhosts/
server {
    listen 80;
    root /home/webtest/;
    access_log /var/log/nginx/ main;
    error_log /var/log/nginx/ warn;

    set $fpmuser webtest;

    if ($bad_bot) { return 444; }

    include conf.d/custom/restrictions.conf;
    include conf.d/custom/pagespeed.conf;
    include conf.d/custom/fpm-wordpress-user.conf;

[root at web ~]# cat /etc/nginx/conf.d/custom/fpm-wordpress-user.conf
location / {
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    try_files $uri $uri/ /index.php?$args;

location ~*
\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|woff)$ {
    expires 2w;
    log_not_found off;

location ~* \.(?:css|js)$ {
    expires 1w;
    add_header Pragma public;
    add_header Cache-Control "public";

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_buffers 8 256k;
    fastcgi_buffer_size 256k;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php-fpm/$fpmuser.sock;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list