Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to setup Nginx to forward requests to several backend services using proxy_pass.

A few of them don't support being accessed under a sub-folder, so I have to add a rewrite to strip the sub-folder which is appended to allow access to them all from the same port.

Any tips on improving the rewrite?

Curl output;

:~$ curl -I -k
HTTP/1.1 404 Not Found
Server: nginx/1.0.5
Date: Thu, 19 Jan 2012 22:30:46 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Content-Length: 145

:~$ curl -I -k
HTTP/1.1 200 OK
Server: nginx/1.0.5
Date: Thu, 19 Jan 2012 22:31:12 GMT
Content-Type: text/html
Connection: keep-alive
Content-Length: 1285
Accept-Ranges: bytes
Last-Modified: Wed, 18 Jan 2012 01:35:21 GMT

Config files;


location /internal {
    rewrite           ^/internal/(.*) /$1 break;
    proxy_pass        http://localhost:8081/internal;
.... more entries ....


server {
    listen 443;

    server_name_in_redirect off;

    include proxy.conf;

    ssl on;

proxy_connect_timeout   59s;
proxy_send_timeout      600;
proxy_read_timeout      600;
proxy_buffer_size       64k;
proxy_buffers           16 32k;
proxy_pass_header       Set-Cookie;
proxy_redirect          off;
proxy_hide_header       Vary;

proxy_busy_buffers_size         64k;
proxy_temp_file_write_size      64k;

proxy_set_header        Accept-Encoding         '';
proxy_ignore_headers    Cache-Control           Expires;
proxy_set_header        Referer                 $http_referer;
proxy_set_header        Host                    $host;
proxy_set_header        Cookie                  $http_cookie;
proxy_set_header        X-Real-IP               $remote_addr;
proxy_set_header        X-Forwarded-Host        $host;
proxy_set_header        X-Forwarded-Server      $host;
proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
proxy_set_header        X-Forwarded-Ssl         on;
proxy_set_header        X-Forwarded-Proto       https;
share|improve this question
up vote 6 down vote accepted

We could turn your rewrite a lot uglier to account for that string (/internal) without allowing unintended matches.. (if you're inclined, you'll want ((?:/.*|)) where your slash is or a similar beast) but ugly is less maintainable.

I'm inclined to say just do this:

location /internal {
    rewrite           ^/internal$ permanent;
    rewrite           ^/internal/(.*) /$1 break;
    proxy_pass        http://localhost:8081/internal;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.