Skip to content

Instantly share code, notes, and snippets.

@jgornick
Last active January 5, 2024 00:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgornick/0b10798608193ba4fd6d to your computer and use it in GitHub Desktop.
Save jgornick/0b10798608193ba4fd6d to your computer and use it in GitHub Desktop.
Nginx: Advanced proxy_pass depending on file exists
server {
listen 443;
server_name my.domain.com;
root /var/www/my.domain.com;
ssl on;
ssl_certificate /usr/local/etc/ssl/star.crt;
ssl_certificate_key /usr/local/etc/ssl/star.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;
location / {
set $path $uri;
if ($http_referer ~* my\.domain\.com(.*)$) {
set $path $1;
}
set $v1_url https://v1.domain.com;
set $v2_url https://v2.domain.com;
set $proxy_pass_url $v1_url;
if (-f "/v2/$path") {
set $proxy_pass_url $v2_url;
break;
}
if (-f "/v2/$path.js") {
set $proxy_pass_url $v2_url;
break;
}
if (-f "/v2/$path.html") {
set $proxy_pass_url $v2_url;
break;
}
if (-f "/v2/$path/index.js") {
set $proxy_pass_url $v2_url;
break;
}
if (-f "/v2/$path/index.html") {
set $proxy_pass_url $v2_url;
break;
}
proxy_pass $proxy_pass_url;
# Pass a bunch of headers to the downstream server, so they'll
# know what's going on.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Secure on;
# Most web apps can be configured to read this header and
# understand that the current session is actually HTTPS.
proxy_set_header X-Forwarded-Proto https;
# We expect the downsteam servers to redirect to the right
# hostname, so don't do any rewrites here.
proxy_redirect off;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment