Reputation: 1856
I've got my nginx setup working nicely, I'd just like to see if I can clean it up by combining all these locations with proxy_pass
directives into one:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend:1337;
}
location /forgotPassword {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend:1337;
}
location /updatePassword {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend:1337;
}
location /register {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend:1337;
}
location /login {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend:1337;
}
location /logout {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend:1337;
}
location ~ ^.+\..+$ {
try_files $uri =404;
}
location / {
try_files $uri /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
As you can see the contents of each of them is identical, the only difference is the prefix.
Is there a logically equivalent way to DRY this up without changing the behavior?
Upvotes: 1
Views: 1636
Reputation: 6864
Try something along these..
server {
<..>
location ~ ^/(api|forgotPassword|updatePassword|register|login|logout) {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend:1337;
}
<..>
}
Upvotes: 2