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










No comments:

Post a Comment

@qzl/typed-i18n — Our Open-Source Typed i18n Project is Gaining Traction

 Hello everyone! 👋 We are excited to share an update about one of our open-source projects , @qzl/typed-i18n , created by the QZ-L.com team...