Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

NGINX Avoid Proxy Pass Duplication for Multiple Locations

I'm writing a reverse proxy for a site.

It work's fine but i'd like to add/modify headers for assets.

If i remove the proxy pass from each location block it stops proxying for the asset blocks, i foolishly assumed that the / root location block would take precedence and apply further down.

I've used try_files $uri @proxy and created a location @proxy block, but it feels ugly with NGINX looking for the files that will never be locally.

I'm asking how i can avoid duplication like this:

    location / {
        proxy_pass https://web1.local;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|ttf|otf|woff|woff2|eot)$ {
        proxy_pass https://web1.local;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        expires max;
        access_log off;
        add_header Cache-Control "public";
        add_header X-Asset "Yes";
    }

    location ~* .(?:css|js)$ {
        proxy_pass https://web1.local;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        add_header X-Asset "Yes";
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...