How to config multi servers(second domain name server) with one server {} in your nginx.conf

config sample:

server {
    listen       80;
    server_name  ~^(.+)?\.errong\.win$;
    if ($host = errong.win) {
        rewrite ^ http://www.errong.win permanent;
    }

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   vhosts/$1;
        index  index.html index.htm;
    }
}



Explaination:

1. my domain name is "errong.win",you should replace it with yours.
2. "vhosts" is my root directory, you should replace it with yours too.
3. The path tree of your root directory should like:


errong_leng@blogvpn:~/local/nginx$ ls vhosts/ -all
total 16
drwxrwxr-x  4 errong_leng errong_leng 4096 May 18 02:57 .
drwxrwxr-x 12 errong_leng errong_leng 4096 May 18 02:12 ..
drwxrwxr-x  2 errong_leng errong_leng 4096 May 18 03:05 blog
drwxrwxr-x  2 errong_leng errong_leng 4096 May 18 02:42 freevpn
lrwxrwxrwx  1 errong_leng errong_leng    4 May 18 02:54 www -> blog

PS: due to below codes in conf,
    if ($host = errong.win) {
        rewrite ^ http://www.errong.win permanent;
    }

http://errong.win will same as http://www.errong.win.
And I make a soft link of www to blog.

Remember reload your nginx server 
sudo sbin/nginx -s reload 

So http://errong.win and http://www.errong.win will show same website like 
http://blog.errong.win










Comments

Popular posts from this blog

How to fix error : no module named sendgrid when try to use sendgrid python lib in PHP.

react-native run-android : sun.security.provider.cert path.SunCertPathBuilderException : unable to find valid certification path to req uested target

react-native run-android : do not build/update modified code(App.js)