Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 medicine.example.com, the subdomain should be mapped to example.com/sites/medicine. So the URL the user sees is medicine.example.com. index.php is stripped off via php.

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

The below code gives me a 502 bad gateway error.

server {
    listen      80;
    listen      443 ssl;
    server_name medicine.example.com;

    location / {
        rewrite             ^([^.]*[^/])$ $1/ permanent;
        proxy_pass_header   Set-Cookie;
        proxy_pass          https://example.com/sites$request_uri;
share|improve this question
Do you want an HTTP redirect or internal rewrite? Is example.com 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 example.com is also served by the same web server –  tim peterson Mar 11 '13 at 18:02

1 Answer 1

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 {
    server_name example.com medicine.example.com;

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

share|improve this answer
hmm, medicine.example.com redirects to example.com 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? gist.github.com/tim-peterson/5136554 –  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.