Friday, October 30, 2015

Wildcard records with Nginx to handle custom domains



Here's my 4, going on 5 hour problem:



I've set up a WordPress multisite instance that's going to be handling sites at domain.com, subdomain.domain.com and customdomain.com. There will be N number of sites using customdomain.com, so I'd prefer not creating records for each. On the server, I have Nginx in front of Apache.



What I'd like to do is set up a wildcard record in Nginx to handle all of the custom domains. Right now, it looks something like this:



server {
listen 80;

server_name _;
root /home/server_user/web/production;
client_max_body_size 50M;
client_body_buffer_size 128k;

location / {
access_log off;
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $proxy_host;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}


With this setup, it will pass requests to Apache and serve the dynamic content but returns 404s for all of the static content. If I change 'server_name' to 'customdomain.com', Nginx starts serving static content again. When I change 'server_name' to '_' or any other catch-all pattern, Nginx falls on its face.



Any ideas?


Answer




The solution (at least with my configuration):



In your wildcard record, the 'listen' directive should also include 'default':



listen 80 default;


Don't add a 'server_name' directive because that will cause things to break in ugly, unexpected ways.



Props to Max Cutler for helping me figure this out.



No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...