Welcome! Log In Create A New Profile

Advanced

How to create an alias in nginx?

Posted by mekosx 
How to create an alias in nginx?
June 01, 2009 12:51PM
Hi, I've just switched to nginx from lighttpd. One thing i can't understand at all is how the hell am I supposed to create an alias to a directory.
For example - default configuration sets main directory to /usr/share/nginx/html. That's OK for me, when I enter http://myserver.pl I see what's under /usr/share/nginx/html, but what should I do to see content of /home/blah/blah/ under http://myserver.pl/blahblahblah without moving stuff from /home/blah/blah to /usr/share/nginx/html/blahblahblah. Hope you understood me.
I'm using nginx 0.6.36 from Fedora repository.
It's probably the simpliest thing in the world and i'm just too stupid to get it. I've read wiki and googled for this - haven't found anything that could help me.
Sorry for my english, It's not my native language ;)



Edited 2 time(s). Last edit at 06/01/2009 12:55PM by mekosx.
Re: How to create an alias in nginx?
June 01, 2009 02:45PM
Don't worry about your English. I understand you fine. :) Twój angielski jest dużo lepiej niż moj polski!

You can create whatever document root you want like this:

[code]
server {
listen 80;
server_name myserver.pl www.myserver.pl;
index index.html;
root /home/myserver.pl;
...
}
[/code]


or, if you want to keep the default, only move some subdirectories:

[code]
server {
...
location /foo/ {
root /home/path/to/foo;
...
}
...
}
[/code]


Now a request for http://myserver.pl/foo/images/123.jpg will return /home/path/to/foo/images/123.jpg.


Using the default in the nginx.conf is fine if there is only one virtual host on the server. Otherwise you need to separate out into separate server blocks like above.

Another option is to create a directory called "sites-enabled" and put a file for each virtual host in it. Then you can add a line at the end of nginx.conf:

[code]
include /path/to/sites-enabled/*;
[/code]

--
Jim Ohlstein
Re: How to create an alias in nginx?
June 30, 2009 11:42PM
Jim Ohlstein Wrote:
-------------------------------------------------------
> Don't worry about your English. I understand you
> fine. :) Twój angielski jest dużo lepiej niż
> moj polski!
>
> You can create whatever document root you want
> like this:
>
>
> server {
> listen 80;
> server_name myserver.pl www.myserver.pl;
> index index.html;
> root /home/myserver.pl;
> ...
> }
>
>
>
> or, if you want to keep the default, only move
> some subdirectories:
>
>
> server {
> ...
> location /foo/ {
> root /home/path/to/foo;
> ...
> }
> ...
> }
>
>
>
> Now a request for
> http://myserver.pl/foo/images/123.jpg will return
> /home/path/to/foo/images/123.jpg.
>
>
> Using the default in the nginx.conf is fine if
> there is only one virtual host on the server.
> Otherwise you need to separate out into separate
> server blocks like above.
>
> Another option is to create a directory called
> "sites-enabled" and put a file for each virtual
> host in it. Then you can add a line at the end of
> nginx.conf:
>
>
> include /path/to/sites-enabled/*;
>


I am sorry! I see your answer and I do it ,but I don't see anything.
this is my config file bellow:
worker_processes 1;
events {
worker_connections 1024;
use epoll;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
[color=#FF0000]location /foo {
root html/foo;
index index.html index.htm;[/color]
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

the red type is added the config file. I copy the file "index.html" to the directory "foo".default document is "/usr/local/webserver/nginx/html".

make a directory "foo" in html.



Edited 1 time(s). Last edit at 06/30/2009 11:49PM by jason_wang.
Re: How to create an alias in nginx?
July 01, 2009 11:39AM
jason_wang Wrote:
>
>
> I am sorry! I see your answer and I do it ,but I
> don't see anything.
> this is my config file bellow:
> worker_processes 1;
> events {
> worker_connections 1024;
> use epoll;
> }
>
> http {
> include mime.types;
> default_type application/octet-stream;
> sendfile on;
> keepalive_timeout 65;
> server {
> listen 80;
> server_name localhost;
> location / {
> root html;
> index index.html index.htm;
> }
> location /foo {
> root html/foo;
> index index.html index.htm;
> }
> error_page 500 502 503 504 /50x.html;
> location = /50x.html {
> root html;
> }
> }
>
> the red type is added the config file. I copy the
> file "index.html" to the directory "foo".default
> document is "/usr/local/webserver/nginx/html".
>
> make a directory "foo" in html.

If I understand what you are trying to do correctly you need to change root to full path to doc root.

So:

root /usr/local/webserver/nginx/html;

or

root /usr/local/webserver/nginx/htm/foo;

--
Jim Ohlstein
Re: How to create an alias in nginx?
July 18, 2009 11:18AM
[code]
location ~ ^/aliaspath/(.*\.php)$ {
alias /real/path/to/file/$1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
}

location ~ ^/aliaspath($|/.*) {
alias /real/path/to/file$1;
}
[/code]


.php (or other non-static content) block should always be placed before the 'generic' block



Edited 1 time(s). Last edit at 07/18/2009 11:18AM by edogawaconan.
Re: How to create an alias in nginx?
July 29, 2013 06:34AM
Hi,

Can you please help me that how to create Alias in nginx.

I have a cake php website. i am running very smoothly though virtual hosting. but i want to create on more instance as alias.

I am not able to understand that how to do that ?

In apache, i can just write easily write Alias /directoryname /path/of/code

But in nginx i am not able to do it.

Please suggest.


Regards

Rupesh



Edited 1 time(s). Last edit at 07/29/2013 06:35AM by rupesh.kumar.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 57
Record Number of Users: 7 on March 06, 2014
Record Number of Guests: 229 on August 01, 2014
Powered by nginx    Powered by FreeBSD    PHP Powered    Powered by Percona     ipv6 ready