Alias in Nginx Config

APseudoUtopia apseudoutopia at gmail.com
Sun Aug 16 00:07:54 MSD 2009


2009/8/1 Igor Sysoev <is at rambler-co.ru>:
> On Sat, Aug 01, 2009 at 10:54:28AM -0400, APseudoUtopia wrote:
>
>> 2009/7/31 Igor Sysoev <is at rambler-co.ru>:
>> > On Thu, Jul 30, 2009 at 05:09:08PM -0400, APseudoUtopia wrote:
>> >
>> >> 2009/7/29 Igor Sysoev <is at rambler-co.ru>:
>> >> > On Tue, Jul 28, 2009 at 04:47:26PM -0400, APseudoUtopia wrote:
>> >> >
>> >> >> 2009/7/26 Igor Sysoev <is at rambler-co.ru>:
>> >> >> > On Sun, Jul 26, 2009 at 02:07:28PM -0400, APseudoUtopia wrote:
>> >> >> >
>> >> >> >> Hey list,
>> >> >> >>
>> >> >> >> How does one create an alias in nginx? Here's my setup:
>> >> >> >>
>> >> >> >> My web-root is /usr/local/www/main. It's a copy of a SVN repo, so I
>> >> >> >> cannot manually add any folders in there.
>> >> >> >> I want to make an alias from domain.com/wiki to /usr/local/www/wiki.
>> >> >> >> I've tried the following code:
>> >> >> >>
>> >> >> >> location = /wiki {
>> >> >> >> alias /usr/local/www/wiki;
>> >> >> >> }
>> >> >> >>
>> >> >> >> But this isn't working. nginx is returning a "404" error. Here's the
>> >> >> >> whole config for the server block:
>> >> >> >>
>> >> >> >> server {
>> >> >> >>     set $web_root /usr/local/www/main;
>> >> >> >>
>> >> >> >>     listen x.x.x.x:443 default accept_filter=httpready;
>> >> >> >>     server_name domain.tld;
>> >> >> >>     access_log /var/log/httpd/nginx.access.ssl.log main buffer=4k;
>> >> >> >>     root $web_root;
>> >> >> >>
>> >> >> >>     # www redirect
>> >> >> >>     if ($host ~* www\.(.*)) {
>> >> >> >>         rewrite ^(.*)$ https://domain.tld$1 permanent;
>> >> >> >>     }
>> >> >> >>
>> >> >> >>     ssl on;
>> >> >> >>     ssl_certificate /usr/local/etc/nginx/ssl.crt;
>> >> >> >>     ssl_certificate_key /usr/local/etc/nginx/ssl.key;
>> >> >> >>     ssl_ciphers -ALL:!ADH:!NULL:!aNULL:!eNULL:HIGH;
>> >> >> >>     ssl_prefer_server_ciphers on;
>> >> >> >>     ssl_protocols SSLv3 TLSv1;
>> >> >> >>     ssl_verify_client off;
>> >> >> >>     ssl_verify_depth 1;
>> >> >> >>     ssl_session_cache shared:NGXSSL:1m;
>> >> >> >>     ssl_session_timeout 5m;
>> >> >> >>
>> >> >> >>     # Wiki
>> >> >> >>     location = /wiki/ {
>> >> >> >>         alias /usr/local/www/skittles_wiki/;
>> >> >> >>     }
>> >> >> >>
>> >> >> >>     location ~ .*\.php$ {
>> >> >> >>         try_files $uri /404.html;
>> >> >> >>         fastcgi_index index.php;
>> >> >> >>         fastcgi_ignore_client_abort off;
>> >> >> >>         fastcgi_intercept_errors off;
>> >> >> >>         fastcgi_pass 127.0.0.1:9000;
>> >> >> >>         fastcgi_read_timeout 10; # sec to wait for php-cgi to return data
>> >> >> >>         fastcgi_param SCRIPT_FILENAME $web_root$fastcgi_script_name;
>> >> >> >>         include /usr/local/etc/nginx/fastcgi_params;
>> >> >> >>     }
>> >> >> >> }
>> >> >> >
>> >> >> > Frist, this
>> >> >> >
>> >> >> >     # www redirect
>> >> >> >     if ($host ~* www\.(.*)) {
>> >> >> >         rewrite ^(.*)$ https://domain.tld$1 permanent;
>> >> >> >     }
>> >> >> >
>> >> >> > should be written as separate server:
>> >> >> >
>> >> >> > server {
>> >> >> >     server_name www.domain.tld;
>> >> >> >
>> >> >> >     rewrite ^(.*)$ https://domain.tld$1 permanent;
>> >> >> > }
>> >> >> >
>> >> >> > Second, "set" in
>> >> >> >
>> >> >> >     set $web_root /usr/local/www/main;
>> >> >> >     ...
>> >> >> >     root $web_root;
>> >> >> >     ...
>> >> >> >         fastcgi_param SCRIPT_FILENAME $web_root$fastcgi_script_name;
>> >> >> >
>> >> >> > is just waste of CPU cycles and should be changed to
>> >> >> >
>> >> >> >     root /usr/local/www/main;
>> >> >> >     ...
>> >> >> >         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
>> >> >> >
>> >> >> > As to wiki, you need to omit "=":
>> >> >> >
>> >> >> >     location /wiki/ {
>> >> >> >         alias /usr/local/www/skittles_wiki/;
>> >> >> >     }
>> >> >> >
>> >> >> >
>> >> >> > --
>> >> >> > Igor Sysoev
>> >> >> > http://sysoev.ru/en/
>> >> >> >
>> >> >> >
>> >> >>
>> >> >> Thanks for the quick reply Igor. However, the changes don't seem to
>> >> >> make any difference; I'm still getting the 404 error. After playing
>> >> >> around with my configuration, it seems to be related to the try_files
>> >> >> inside the "location ~ .*\.php$" block. Do you (or anyone else) know
>> >> >> of a way I can solve this? It's a big confusing.
>> >> >
>> >> > What is in error_log ?
>> >> >
>> >> >
>> >> > --
>> >> > Igor Sysoev
>> >> > http://sysoev.ru/en/
>> >> >
>> >> >
>> >>
>> >> This is in the error log:
>> >>
>> >> 2009/07/30 21:07:18 [error] 85246#0: *3 open()
>> >> "/usr/local/www/main/404.html" failed (2: No such file or directory),
>> >> client: x.x.x.119, server: domain.tld, request: "GET /wiki/ HTTP/1.1",
>> >> host: "domain.tld"
>> >> 2009/07/30 21:07:18 [error] 85246#0: *3 open()
>> >> "/usr/local/www/main/404.html" failed (2: No such file or directory),
>> >> client: x.x.x.119, server: domain.tld, request: "GET /wiki/ HTTP/1.1",
>> >> host: "domain.tld"
>> >>
>> >> This also leads me to believe that it has to do with try_files somehow.
>> >
>> > Could you show full current configuration ?
>> >
>> > Also, where should reside the files /wiki/*.php and /*.php at ?
>> >
>> >
>> > --
>> > Igor Sysoev
>> > http://sysoev.ru/en/
>> >
>> >
>>
>> The full configuration is at:
>> http://somerandomguys.info/nginx.conf
>>
>> The main site files are in /usr/local/www/site_main/ and the wiki
>> files are in /usr/local/www/site_wiki/.
>>
>> I want https://domain.tld/ to go to the main site files, and
>> https://domain.tld/wiki/ to go to the wiki files.
>
> Probably, you need something like this:
>
> server {
>
>    ...
>
>    fastcgi_ignore_client_abort off;
>    fastcgi_intercept_errors off;
>    fastcgi_read_timeout 10;
>
>    index   index.php;
>
>    location /wiki/ {
>        alias /usr/local/www/site_wiki/;
>    }
>
>    location ~ ^/wiki/.+\.php$ {
>        alias /usr/local/www/site_wiki/;
>        try_files $uri /404.html;
>        fastcgi_pass 127.0.0.1:9000;
>        fastcgi_param SCRIPT_FILENAME   $request_filename;
>        include /usr/local/etc/nginx/fastcgi_params;
>    }
>
>    location / {
>        root  /usr/local/www/site_main/;
>    }
>
>    location ~ \.php$ {
>        root  /usr/local/www/site_main/;
>        try_files $uri /404.html;
>        fastcgi_pass 127.0.0.1:9000;
>        fastcgi_param SCRIPT_FILENAME   $request_filename;
>        include /usr/local/etc/nginx/fastcgi_params;
>    }
>
>> Thank you very much for your time with this.
>
> BTW, "worker_connections 64" are too small for 2 workers. This means
> that server is able to serve just 128 connections. This is not enough
> in modern world.
>
>
> --
> Igor Sysoev
> http://sysoev.ru/en/
>
>

Sorry for the long wait in the reply, I was away on vacation.

I tried using the configuration from the last email, however, I got
the following error:

[emerg]: the "alias" directive must use captures inside location given
by regular expression in /usr/local/etc/nginx/nginx.conf:154

I tried changing that line to "root" instead of "alias", but it still
does not work. I get the following in the error_log:

2009/08/15 19:29:02 [error] 66344#0: *94 open()
"/usr/local/www/site_main/wiki" failed (2: No such file or directory),
client: x.x.x.215, server: domain.tld, request: "GET /wiki HTTP/1.1",
host: "domain.tld"

This is frustrating me because I've played with different ways to
setup this alias, but I can't seem to figure out how to actually do
it. It seems like it would be very easy and quick to accomplish.

Thanks.





More information about the nginx mailing list