Reputation: 7172
I have the following config file.
server {
listen 80;
server_name web.example.com;
access_log /var/www/example/shared/log/web.access.log;
error_log /var/www/example/shared/log/web.error.log debug;
rewrite ^/(.*)$ http://www.example.net$request_uri; permanent;
}
When I make a request for curl -Ii -H "Host: example.com" http://example.com the above rewrite rule works. (ARGH)..
The server_name explicitly says "web.example.com"
2014/11/18 22:49:20 [notice] 30694#0: 1868 "^/(.)$" matches "/", client: 1.2.3.4, server: web.example.com, request: "HEAD / HTTP/1.1", host: "example.com" 2014/11/18 22:49:20 [notice] 30694#0: *1868 rewritten redirect: "http://www.example.net/", client: 1.2.3.4, server: web.example.com, request: "HEAD / HTTP/1.1", host: "example.com"
Not present here is the other server { } configs. Xavier (below) pointed out that I had set default_server for listen: 443; but not for listen: 80. (argh)
Upvotes: 0
Views: 137
Reputation: 2632
That's not the strict solution to the issue.
What's happening is that you have only one server block and it becomes the default server block for all requests, even the ones not matching the server name. You simply need to add a default server block in your configuration :
server {
listen 80 default_server;
}
By the way, you have a typo (semicolon before permanent
) and you don't need a rewrite as you have specific regular expression. Use this instead :
return 301 http://www.example.net$request_uri;
Upvotes: 1
Reputation: 7172
After a while...
I found that I needed the location / { } wrapped around it
server {
listen 80;
server_name web.example.com;
access_log /var/www/example/shared/log/web.access.log;
error_log /var/www/example/shared/log/web.error.log debug;
location / {
rewrite ^/(.*)$ http://www.example.net$request_uri permanent;
}
}
Upvotes: 0