Newbie questions about nginx (moving from apache)

Maxim Dounin mdounin at mdounin.ru
Sat May 28 18:32:35 MSD 2011


Hello!

On Sat, May 28, 2011 at 09:55:10AM -0400, pk899 wrote:

> Thanks. But I am a bit confused, as the simple setup is not working from
> port 81 (for testing): 
> 
> 
>   server {
>     listen 81;
>     server_name MYDOMAIN.com www.MYDOMAIN.com;
> 
>     location ^~ /site/static {
>       root /home/MYDOMAIN/www/static;
>       index index.htm index.php;
>       expires       30d;
>       gzip          on;
>     }
>     
>     location / {
>       proxy_pass      http://127.0.0.1:80/;
>       proxy_redirect  off;
>       
>       proxy_set_header   Host             $host;
>       proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
>     }
> 
>   }
> 
> 
> 
> Then I try in my browser this: 
> http://MYDOMAIN.com:81/test.gif    
> 
> On the server the "test.gif" file is at
> "/home/MYDOMAIN/www/static/test.gif". 
> 
> Questions: 
> 
> (1) What am I missing? Should the test.gif file be somewhere else?
> Should I try this with some other URL instead of the one I mention
> above? 

It looks like you don't understand at least two basic things:

1. Location directive maps *URI* namespace to configuration.  
That is, with your config /test.gif will be mapped to "location 
/", i.e. will be proxy_pass'ed to Apache.  You want to request 
something like /site/static/test.gif to be actually served by 
nginx itself.

See here for more details:

http://wiki.nginx.org/HttpCoreModule#location

2. Root directive define *root*, and URI will be added to it to 
map request to filesystem.  I.e. with URI /site/static/test.gif 
and root /home/MYDOMAIN/www/static you will end up with 
"/home/MYDOMAIN/www/static/site/static/test.gif" filename.

If you want "/site/static/test.gif" to be mapped to 
"/home/MYDOMAIN/www/static/test.gif" you have to use alias 
directive instead.

See here for more details:

http://wiki.nginx.org/HttpCoreModule#root
http://wiki.nginx.org/HttpCoreModule#alias

> (2) Also, if this worked, and I accepted Nginx to be at the front and
> moved Apache to the background, isn't it true that Apache would have to
> pass back all processed output to Nginx? 

Yes.  And this is a huge win in many cases: Apache processes won't 
be bound by serving responses to slow clients.

> (3) Even with "proxy_redirect off", when I try http://MYDOMAIN.com:81,
> it actually does a hard 301 redirect to my Apache server. 

Directive proxy_redirect is to fix redirects returned by backend 
server.  By switching it off you merely said "don't touch 
anything" to nginx, and this is what it does.  I.e. redirect is 
returned by your Apache server (alternatively, it may be just your 
browser's cache from previous testing - check logs).

Maxim Dounin



More information about the nginx mailing list