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 am using nginx for request forwarding on my server.I have two server blocks,one for the main website and the other server block to serve my S3 static content.

   server {
      listen 80;
      server_name abc.guru www.abc.guru;

       access_log  off;
       error_log off;

      location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        Host            $host;
        proxy_redirect          off;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
      }
 }

 # Server for S3  This accepts anything apart from the above two
server {
    # Listen on port 80 for all IPs associated with your machine
    listen 80 default_server;

    # Catch all other server names
    server_name _;
 }

This works for abc.guru but if I enter www.abc.guru it fails i.e gives error This webpage has a redirect loop. I referred documetation and it says we can add multiple domains using space.

I have my DNS record for www @ and @ refers to the root ip address.

How can I get it working for both in the first server block and for any other domain in the second server block?

In my .htaccess file I have

 RewriteRule (.*) http://www.abc.guru/$1 [R=301,L]
 RewriteRule ^http://www.abc.guru/$1  agent.guru [L]
 RewriteRule ^http://www.abc.guru/$1  index.php [L]
share|improve this question
    
nginx doesn't have any redirects defined. Check your application. –  Michael Hampton Feb 26 at 14:01
    
sorry sir,I did not get.Can you please elaborate a bit.Do you mean there is no issue in the config above? –  KillABug Feb 26 at 14:02

2 Answers 2

The culprit here is the first RewriteRule in your Apache configuration. It tells Apache to redirect every request to http://www.abc-guru.com.

So, when nginx sends a request to http://www.abc-guru.com. the request is routed to Apache, and the first RewriteRule redirects that request to http://www.abc-guru.com, thus causing a redirect loop.

The best solution is to migrate the redirects to nginx, like xxdesmus suggested.

Another option is to restrict the first RewriteRule to be applied only when the HTTP Host header is abc-guru.com with a RewriteCond directive.

share|improve this answer

Instead of handling your redirects in your Apache htaccess file, why not just handle those redirects directly in Nginx instead? Forward root -> www with a 301 redirect, and then in the www server_name block send that to your proxy_pass (Apache)?

share|improve this answer
    
I did not get you sir.I will explain my scenario.I use nginx as a proxy server and it proxies all requests to apache on the same machine.As apache is not powerful enough to handle it had to go with nginx.Nginx is basically to display static html pages in my s3 bucket –  KillABug Feb 27 at 11:26

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.