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 know this question has been asked before on SO but I'm having trouble replicating it on my server.

What I'm trying to do is when the user goes to, the subdomain should be mapped to So the URL the user sees is index.php is stripped off via php.

Ideally, the solution would be a wildcard/regex-based such that more than just could be mapped to url.

The below code gives me a 502 bad gateway error.

server {
    listen      80;
    listen      443 ssl;

    location / {
        rewrite             ^([^.]*[^/])$ $1/ permanent;
        proxy_pass_header   Set-Cookie;
share|improve this question
Do you want an HTTP redirect or internal rewrite? Is served by the same webserver? – mgorven Mar 11 '13 at 16:42
hi @mgorven, im looking for an internal rewrite, I want to keep the subdomain as the URL, yes is also served by the same web server – tim peterson Mar 11 '13 at 18:02

You don't really want to proxy requests to the same webserver, it requires more overhead. I would configure a single virtual host for both domains, and use an if statement to only rewrite requests for the subdomain.

server {

    if ($host = "") {
        rewrite . /sites/medicine$request_uri;

share|improve this answer
hmm, redirects to regardless of whether I put your rewrite condition in. So it looks like I have a problem somewhere else in my nginx.conf. Here's my full nginx.conf if that's helpful. Also, is proxy request the same as internal rewrite? – tim peterson Mar 11 '13 at 18:45
@timpeterson Then something else in your config is redirecting. Also check /etc/nginx/sites-enabled/ for config. Proxying is not the same as rewriting -- proxying should only be used when forwarding to a different web server. – mgorven Mar 11 '13 at 19:39
@mgovern thanks, my /etc/nginx/sites-enabled is empty so I'm pretty sure my nginx.conf is the only thing that is contributing. Would mind taking a look at the nginx.conf file i included as a gist? – tim peterson Mar 11 '13 at 19:50
@timpeterson I can't spot anything which would cause that. Try commenting out rewrite statements until it stops doing that. – mgorven Mar 11 '13 at 21:18

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.