How to configure multiple sites on nginx and bind different domain names

In the daily development process, in order to save server resources, we will configure multiple domain names on a web server to improve resource utilization.

How can I place multiple sites on one server without affecting each other?

Let me tell you!

1. Preliminary work

  • A web server with lnmp ( Linux+Nginx+MySql+PHP ) installed
  • Two domain names
    • www.linuxcommands.site
    • www.linuxcommands.site
  • System: ubuntu 18.04

2. Now two domain names need to correspond to different directories without affecting each other

www.linuxcommands.site    /data/wwwroot/default/linuxcommands/
www.linuxcommands.site     /data/wwwroot/default/codingfailure/

3. Nginx configure virtual host

Before adding the configuration file, open ngin.conf to see if there is this sentence:

➜ vim nginx.conf

...
include vhost/*.conf;

If not, add this configuration and create a vhost directory.

Enter the vhost directory and create two corresponding domain name configuration files.

www.linuxcommands.site.conf
www.linuxcommmands.site.conf

The naming format of the virtual host configuration file must be the same as the end of the nginx.conf configuration added above.

Here ends with .conf

Configure the virtual host configuration file for the domain name www.linuxcommands.site.

# www.linuxcommmands.site.conf
server {
  server_name  www.linuxcommands.site linuxcommands.site;
  access_log /data/wwwlogs/www.linuxcommands.site_nginx.log combined;
  index index.html index.htm index.php;
  root /data/wwwroot/default/linuxcommands/;
  
  include /usr/local/nginx/conf/rewrite/none.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;

  location ~ [^/]\.php(/|$) {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
  }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
  }
  location ~ /\.ht {
    deny all;
  }
}

The domain name www.linuxcommands.site is similar, you only need to modify the domain and root sections in the above configuration.

4. Verify that the two domain names are configured successfully.

1. Add hosts mapping on this machine

➜  ~ vim /etc/hosts

100.16.23.211 www.linuxcommands.site
100.16.23.211 www.linuxcommmands.site

2. Restart Nginx service

➜  ~ service nginx restart

After completing the above steps, you can view it through your browser.

5. The most important step is to add domain name resolution to your domain name service provider.

Ok, it’s done.

You can use one server to support two domain names, and of course you can support more as long as your server performance is better.

Add a Comment

Your email address will not be published. Required fields are marked *