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 have a problem where Nginx redirects me to the wrong url if there is no trailing / provided.

I have haproxy setup to delegate the requests between multiple servers.

curl output:

➜  ~  curl -i
HTTP/1.1 301 Moved Permanently
Server: nginx/1.1.19
Date: Tue, 14 Aug 2012 08:10:39 GMT
Content-Type: text/html
Content-Length: 185
Connection: close

<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>

Nginx config:

server {
    listen *:4000;
    access_log  /var/log/nginx/website-com.log;
    rewrite_log on;
    error_page 405 = $uri;

    location / {
      root   /home/website/en/;
      index  index.html index.htm;
    if ($host != '' ) {
        rewrite  ^/(.*)$$1  permanent;
    rewrite /registrieren/ permanent;
    rewrite /presse/ permanent;


I have already tried using without success.

share|improve this question
if ($host != '' ) {
    rewrite  ^/(.*)$$1  permanent;

This is a ineffective way. See:

The right way is to define a separate server to catch all other domains. Server names may be defined using regular expressions. Try this:

server {
    listen      *:4000;

server {
    listen      *:4000;
    server_name ~^(?!www)\.ordify\.com$;
    return      301$request_uri;

?!www - negative lookahead, means that match something not followed by www.

share|improve this answer
Does not answer question from OP about trailing slash rewrite. – pkhamre Aug 14 '12 at 10:12

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.