1-- [high-performance Nginx server] - 4 Nginx virtual host configuration

0 use Notepad++to modify configuration files

Here Insert Picture DescriptionHere Insert Picture Description

connection succeeded:

Here Insert Picture Description

You can be directly Notepad++modified the configuration file, save will sync to Linux

1 NginxVirtual Host Configuration

  1. 基于域名的虚拟主机By domain name to distinguish between virtual hosts - Application: External website.
  2. 基于端口的虚拟主机To distinguish between virtual hosts on port - Application: the company's internal Web site, external website management background.
  3. 基于 ip 的虚拟主机 Almost no.

That is, a server start multiple sites.
How to distinguish between different sites:

  • 域名不同
  • 端口不同

Nginx Profile:
/usr/local/nginx/conf/nginx.conf

1.1 By 域名distinguishing Hosting

Modify window hosts file:

192.168.153.11 www.test.com
192.168.153.11 www.test1.com
192.168.153.11 www.test2.com

Nginx Configuration


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;                 # server监听的端口号
        server_name  localhost;          # 配置域名

        location / {
            root   html;                 # 拦截后,跳转根路径
            index  index.html index.htm; # 默认页面
        }

    }
	
	############ 添加配置一
	server {
        listen       80;
        server_name  www.test1.com;          # 配置域名

        location / {
            root   data/test1;               # 拦截后,跳转根路径 data/test1
            index  index.html index.htm;
        }

    }
	
	############ 添加配置二
	server {
        listen       80;
        server_name  www.test2.com;          # 配置域名

        location / {
            root   data/test2;               # 拦截后,跳转根路径 data/test2
            index  index.html index.htm;
        }

    }

}

Note: Jump root path after the interception should be configured dimension relative path:data/test2

Create a directory under Nginx:

Here Insert Picture Description

Reload the configuration file

/usr/local/nginx/sbin/nginx -s reload

test:

Here Insert Picture Description
Here Insert Picture Description

1.2 By 端口distinguishing different virtual hosts

Add Virtual Host:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;                 # server监听的端口号
        server_name  localhost;          # 配置域名

        location / {
            root   html;                 # 拦截后,跳转根路径
            index  index.html index.htm; # 默认页面
        }

    }
	
	############ 添加配置一
	server {
        listen       8080;                   # 配置监听的端口号
        server_name  www.test.com;

        location / {
            root   data/test1;               # 拦截后,跳转根路径 data/test1
            index  index.html index.htm;
        }

    }
	
	############ 添加配置二
	server {
        listen       8081;                   # 配置监听的端口号
        server_name  www.test.com;

        location / {
            root   data/test2;               # 拦截后,跳转根路径 data/test2
            index  index.html index.htm;
        }

    }

}

Reload the configuration file

/usr/local/nginx/sbin/nginx -s reload

test:

Here Insert Picture Description
Here Insert Picture Description

Published 675 original articles · won praise 214 · Views 140,000 +

Guess you like

Origin blog.csdn.net/weixin_42112635/article/details/104923770