Using variable in vhost

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view

Using variable in vhost

Karl Johnson-2

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

nginx mailing list
[hidden email]
Reply | Threaded
Open this post in threaded view

Re: Using variable in vhost


I don't see any reason why it can be slower since the recommended ways to
pass params to fastcgi is by using dynamics variables ex: fastcgi_param
SCRIPT_FILENAME $document_root$fastcgi_script_name;

The way you're setting your variable is static by the website, the value is
not dynamically linked to the request. So it might have a sight overhead
when starting the web server for parsing the config, instead of having the
full config repeated for each virtual host, but overall once the "virtual
server" is loaded into memory it's shouldn't be a problem.


Posted at Nginx Forum:,280128,281527#msg-281527

nginx mailing list
[hidden email]