How to bracket two similar location-block into one?

by MiXT4PE   Last Updated August 14, 2019 20:00 PM

The following location-blocks seem like a redundancy that can be mitigated - How?

location / {
    proxy_pass http://127.0.0.1:8443;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

location ~* \.io {
    proxy_pass         http://127.0.0.1:8443;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection 'upgrade';
    proxy_set_header   Host $host;
    proxy_cache_bypass $http_upgrade;
}
Tags : nginx


Answers 1


Store the common configuration in a separate .conf file:

/etc/nginx/config/proxy.conf:

proxy_pass http://127.0.0.1:8443;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;

location blocks include the common proxy.conf-:

location / {
   include /etc/nginx/conf/proxy.conf;
}

location ~* \.io {
   include /etc/nginx/conf/proxy.conf;
}
suspectus
suspectus
August 14, 2019 18:24 PM

Related Questions


nginx location not opening static site

Updated August 03, 2019 14:00 PM


Nginx: alias and try_files with $args

Updated March 26, 2015 14:00 PM