“proxy_pass” cannot have URI part in location given by regular expression


I've developed a URL shortening web application.

It consists of two separate docker containers: one containing the backend REST api and another containing the frontend static website.

These two containers are linked to an nginx container. The configuration for this nginx container is below:

worker_processes 1; events { worker_connections 1024; } http { upstream api { server short-url:8080; } upstream frontend { server short-url-frontend:8081; } gzip on; gzip_vary on; gzip_min_length 860; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml application/javascript application/x-javascript application/xml; gzip_disable "MSIE [1-6]\."; server { listen 80; root /user/share/nginx/html; location /urlshortener/v1 { proxy_pass http://api/urlshortener/v1; proxy_redirect off; 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-Host $server_name; } location ~ ^/([A-Za-z0-9]+) { rewrite ^/([A-Za-z0-9]+) /$1 proxy_pass http://api/urlshortener/v1; } location / { proxy_pass http://frontend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-for $remote_addr; } } }

If a url ends with /urlshortening/v1, I'm proxying to the backend.

If a url starts with /, I'm proxying to the frontend.

Shortened urls e.g. /3xTy or /a0q need to be proxied to the backend so that the user can be navigated to the original url. In order to do this, I've defined a location with a regular expression.

location ~ ^/([A-Za-z0-9]+) { rewrite ^/([A-Za-z0-9]+) /$1 proxy_pass http://api/urlshortener/v1; }

This block of code gives me the following error:


2018/11/17 16:47:03 [emerg] 1#1: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/nginx.conf:36


I've gone through several <a href="https://www.thegeekstuff.com/2017/05/nginx-location-examples/" rel="nofollow">examples</a> and reviewed a number of <a href="https://serverfault.com/a/693951" rel="nofollow">answers</a> and I believe that the configuration I have should work. Can someone please explain why I'm getting this error?


If you use a URI with a proxy_pass statement within a <em>regular expression</em> location, you need to build the entire URI using one or more variables. See <a href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass" rel="nofollow">this document</a> for details.

So the alternatives are to (1), capture the URI from the location expression and add it to the proxy_pass statement. For example:

location ~ ^/([A-Za-z0-9]+) { proxy_pass http://api/urlshortener/v1/$1; }

Or (2), use proxy_pass without a URI part, and construct the desired URI using a rewrite...break. For example:

location ~ ^/([A-Za-z0-9]+) { rewrite ^/([A-Za-z0-9]+) /urlshortener/v1/$1 break; proxy_pass http://api; }

See <a href="http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite" rel="nofollow">this document</a> for details.


  • How to get React Table Row Data Onclick
  • iisnode not able to access file present in network file path
  • Why \\z for regular expression doesn't work for me?
  • Convert char pos of UnicodeString to byte pos in a utf8 string
  • What's the easiest way to redirect to the previous page with PHP?
  • How to disable enter button in a form?
  • Adobe air mobile - softKeyboardType is not working when using skinClass to allow scrolling?
  • NSInvalidArgumentException - 'Invalid top-level type in JSON write' - Swift
  • How to check if “set” in c
  • How can I handle exit() calls in 3rd party library code?
  • can we store negative (-) and plus (+) sign numbers in mysql varchar column
  • Pass variable to a detail view controller using a table in UIViewController (not UITableViewControll
  • Enabling/Disabling row in a datagrid(MVVM pattern)
  • about iOS target version and SDK version
  • spring security - custom filter positioning
  • pass php array to jquery
  • JavaFX resize children relative to parent
  • Intersection of characters in two strings
  • Multithread python requests [duplicate]
  • Force outer SELECT to fail if the inner SELECT contains an invalid identifier
  • Issues with converting data into a matrix after running lapply()
  • how to add dashed border on highcharts “area” graph for every point
  • Swift manually rotate view controller
  • What does “T extends Junk” mean in a generic class in Java?
  • How to make Rss News Reader application in android …? [closed]
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • How do I use libcurl to printf a remote FTP directory listing?
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • How to clear a browser cache in Protractor
  • how to snap two objects in runtime in unity?
  • read part of h5 dataset python
  • Debug `Unexpected end of JSON input Error` on content script
  • Write to .csv file with PHP (Commas in Data Error)
  • ReferenceError: TextEncoder is not defined
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}