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

I'm a bit confused. I have nginx running on a port other than 80.

# part of nginx.conf
server {
  listen  7000;
  include /etc/nginx/my_app.conf;
}

That's because my HAProxy is running on 80 on the same server.

# Approximate haproxy.cfg
listen foo 0.0.0.0:80
  option forwardfor
    server web01 web01:7000 maxconn 25000 check

How do I make nginx serve a redirect from www.example.com to example.com?

I tried the following, but nginx complains that it can't listen on 80 (which makes sense, since haproxy is listening there.)

server {
  server_name  www.example.com;
  rewrite ^ http://example.com$request_uri? permanent;
}
share|improve this question
add comment

1 Answer

Figured it out. Since nginx cascades down the defined servers, I can add a more specific server declaration above the one I had. In my case it's the following.

server {
  listen 7000;
  server_name www.example.com;
  rewrite ^ http://example.com$request_uri? permanent;
}

server {
  listen 7000;
  include /etc/nginx/my_app.conf;
}

I can have multiple declaration listening on the same port, but the first one will catch the www subdomain and serve a redirect, while the second one actually serves the app.

share|improve this answer
add comment

Your Answer

 
discard

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.