Nginx Location Wildcard Proxy_Pass


nginx proxy_pass wildcard config - Server Fault

nginx proxy_pass wildcard config – Server Fault

I have a apache configuration like so:

ProxyPass /app localhost:8080/app
ProxyPassReverse /app localhost:8080/app
ProxyPass /images localhost:8080/app/images
ProxyPassReverse /images localhost:8080/app/images

I am trying to translate this to nginx config. So far I have
server {
listen 80;
server_name localhost;
location /app {
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass localhost:8080/app;}
location /app/images {
proxy_pass localhost:8080/app/images;}… }
This is a lot of repetition. Please help me figure out how to have just one location that would handle all sub-paths also. In addition I would like to not enable any (wildcard) sub-paths, but a set of known ones. Say, I want to enable localhost/app, localhost/app/images, and no others. Just like above but shorter. Is this possible?
asked May 14 ’17 at 5:24
nginx allow having regexp for matching location, assuming you might need Otherwise just remove it.
location ~ ^/app/(|images)? {
proxy_pass localhost:8080;}… }
answered May 14 ’17 at 6:41
TombartTombart1, 8032 gold badges24 silver badges43 bronze badges
You can simply use a location block like this:
proxy_pass localhost:8080;}
When the proxy_pass contains no path component, nginx will append the normalized URI of the request to the host part of proxy_pass directive.
answered May 14 ’17 at 6:43
Tero KilkanenTero Kilkanen30. 1k3 gold badges37 silver badges54 bronze badges
Not the answer you’re looking for? Browse other questions tagged nginx or ask your own question.
examples for how does nginx proxy_pass map the request

examples for how does nginx proxy_pass map the request

16 Sep 2013
1. about
The proxy_pass directive sets the address of the proxied server and the URI to which location will be mapped.
Here are some examples to show how the request URI will be mapped.
The version of nginx:
nginx version: nginx/1. 4. 2
The server config:
#server config
server {
listen 80;
2. location without regular expression
If the proxy_pass directive is specified without a URI,
location /app/ {
If the proxy_pass directive is specified with a URI:
Forward the requested Host header
By default, the Host header from the request is not forwarded, but is set based on the proxy_pass statement. To forward the requested Host header, it is necessary to use:
proxy_set_header Host $host;
3. location with regular expression
If the location is given by regular expression, can not be a URI part in proxy_pass directive, unless there are variables in the directive.
location ~ ^/app/(. *)$ {
# proxy_pass; # error
proxy_pass 1; # ok}
Update from the comment by @Chris Young
variables in proxy_pass directive:
proxy_pass 1;}
with a rewrite directive in the location:
If the rewrite rule is hit, the URI specified in the directive is ignored and the full changed request URI is passed to the server:
rewrite ^/app/hit/(. *)$ /$1 break;
/app/hit/some/request/? name=xxxxx
/app/not_hit/some/request/? name=xxxxx
proxy_pass in nginx wiki
comments powered by
Nginx wildcard proxy, pass subdomain to the server (upstream ...

Nginx wildcard proxy, pass subdomain to the server (upstream …

I would like to be able to pass to apache server, with subdomain info too.
I would like to make a nginx cache for domain, acting like wildcard, but passing subdomain to the destination (there is apache witch wildcard too). Up to now, I pass the info via proxy_set_header Host $host; but I would like to have request with subdomain at the apache server.
upstream {
server 172. 1. 1:80 weight=50 fail_timeout=30s;}
server {
server_name *;
location / {
proxy_pass #proxy_pass $request;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;}
location ~* ^. +. (jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf)$ {
proxy_pass proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache my-cache;
proxy_cache_valid 200 302 30m;
proxy_cache_valid 404 1m;}
access_log /var/log/nginx/ main;
error_log off;}
Do you think I can use proxy_pass with upstream?
Nginx (*) –(cache)–> Apache (*)
Nginx () –(cache)–> Apache ()
tshepang11k21 gold badges86 silver badges128 bronze badges
asked Oct 18 ’12 at 8:45
upstream somestring {
server weight=50 fail_timeout=30s;}
listen 80;
server_name ~^(? . +)\\$;
proxy_pass somestring;
proxy_set_header Host $;}}
answered Dec 25 ’14 at 1:44
dmytrivvdmytrivv5687 silver badges16 bronze badges
So I was trying to find the answer to this problem and kept finding this post. But I think dmytrivv answer is out of date. In our scenario, we have both wildcard domains (e. g. *. ) and custom domains (e. ). But you can solve both by using proxy_set_header Host $host; and having default at the end of your listen.
upstream qaweb {
# Servers in the web farm
server ternal:80;}
listen 443 ssl default;
ssl_certificate certs/;
ssl_certificate_key certs/;
# Support for wildcard domains
server_name *. “”;
# Turn off access logging so we don’t fill the hardrive
access_log off;
proxy_pass qaweb;
proxy_set_header Host $host;
# So that the correct IP shows up in the log once libapache2-mod-rpaf is installed
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
Note, we are also using it as a TLS termination proxy.
You can also find more examples on how to use proxy_pass here
answered Mar 1 ’17 at 16:51
Not the answer you’re looking for? Browse other questions tagged nginx wildcard wildcard-subdomain or ask your own question.

Frequently Asked Questions about nginx location wildcard proxy_pass

About the author


If you 're a SEO / IM geek like us then you'll love our updates and our website. Follow us for the latest news in the world of web automation tools & proxy servers!

By proxyreview

Recent Posts

Useful Tools