Home  >  Article  >  How to configure multiple sites on Nginx

How to configure multiple sites on Nginx

无忌哥哥
无忌哥哥Original
2018-06-27 15:26:263046browse

Sometimes you want to run different sites for different domain names on one server. For example, www.siteA.com serves as a blog and www.siteB.com serves as a forum. You can resolve the IPs of both domain names to your server, but you cannot run two different websites at the same time in the root directory of Nginx. At this time, you need to use a virtual directory. Suppose you put your blog under "/home/user/www/blog" and your forum under "/home/user/www/forum". Now we start the configuration:

Create a "vhost" directory in the Nginx configuration directory. This example assumes that Nginx is installed by default and the configuration directory is in "/etc/nginx"

$ sudo mkdir /etc/nginx/vhost

Create the configuration file of siteA

$ sudo vi /etc/nginx/vhost/vhost_siteA.conf

Enter the following configuration information

server {
    listen       80;                        # 监听端口
    server_name www.siteA.com siteA.com;    # 站点域名
    root  /home/user/www/blog;              # 站点根目录
    index index.html index.htm index.php;   # 默认导航页 
    location / {        # WordPress固定链接URL重写
        if (!-e $request_filename) {
            rewrite (.*) /index.php;
        }
    } 
    # PHP配置
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

Create the configuration file of siteB in the same way as siteA. The only difference between the two is Is the "server_name" and "root" directories

$ sudo vi /etc/nginx/vhost/vhost_siteB.conf

server {
    ...
    server_name www.siteB.com siteB.com;    # 站点域名
    root  /home/user/www/forum;             # 站点根目录    ...
}

Open the nginx.conf file

sudo vi /etc/nginx/nginx.conf

Add the configuration file of the virtual directory to the end of the "http {}" section

http {
...
include /etc/nginx/vhost/*.conf;
}

Restart Nginx service

$ sudo service nginx restart

Now Visit www.siteA.com and www.siteB.com, you will find that the browser will open different sites

Tips for prohibiting access

If your Nginx root directory is set in "/home /user/www", you want to prevent others from accessing your site through "http://IP address/blog" or "http://IP address/forum", the simplest way is to ban IP address access. The method is as follows:

Open the Nginx website default configuration file, remember to back it up first

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default_bak
$ sudo vi /etc/nginx/sites-available/default

Delete all content, leaving only the following configuration

server {
    listen 80 default_server;
    server_name _;    return 404;
}

After restarting Nginx, others will not be able to access through the IP address Website

If you don’t want to ban IP addresses from accessing the entire directory, you just want to prevent others from accessing your blog and forum via IP. Then you need to prohibit directory access to "/blog" and "/forum".

Open the Nginx website default configuration file, the same as above, remember to back it up first

Add the following configuration in the "server { }" section

location ^~ /blog/ {
    deny all;
    }
location ^~ /forum/ { 
   deny all;
   }

Restart Nginx

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn