search
HomeheadlinesHow to configure multiple sites on Nginx

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool