Nginx 核心配置-单节点实现多域名访问

              Nginx 核心配置-单节点实现多域名访问

                                       作者:尹正杰

 

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.试验环境说明

1>.虚拟机环境说明

[[email protected] ~]# uname -r
3.10.0-957.el7.x86_64
[[email protected] ~]# 
[[email protected] ~]# uname -m
x86_64
[[email protected] ~]# 
[[email protected] ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[[email protected] ~]# 
[[email protected] ~]# grep 172.30.1.101 /etc/hosts
172.30.1.101 pc.yinzhengjie.org.cn mobile.yinzhengjie.org.cn mail.yinzhengjie.org.cn
[[email protected] ~]#

 2>.试验环境说明

在同一台web服务器上基于不同的主机名访问不同的网页内容,具体要求如下:
    访问pc.yinzhengjie.org.cn会得到一个网页内容:
      网页内容自定义。
访问mobile.yinzhengjie.org.cn和mail.yinzhengjie.org.cn内容相同:       网页内容自定义即可。

3>.Nginx源码方式安装步骤

博主推荐阅读:
    https://www.cnblogs.com/yinzhengjie/p/12031651.html

 

二.编辑配置文件并重启服务

1>.编辑nginx的主配置文件

[[email protected] ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 

events {
    worker_connections  100000;
    use epoll;
    accept_mutex on;
    multi_accept on; 
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    gzip  on;
    charset utf-8;
    keepalive_timeout  65 65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     
   #导入其他路径的配置文件
   include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}

[[email protected] ~]# 
[[email protected] ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[[email protected] ~]# 

2>.编辑nginx的子配置文件 

[[email protected] ~]# cat /yinzhengjie/softwares/nginx/conf.d/pc.conf 
server {
    listen 80;
    server_name pc.yinzhengjie.org.cn;

    location / {
        root /yinzhengjie/data/web/nginx/html/pc;
        index index.html;
    }

}
[[email protected] ~]# 
[[email protected] ~]# cat /yinzhengjie/softwares/nginx/conf.d/mobile.conf 
server {
    listen 80;
    server_name mobile.yinzhengjie.org.cn mail.yinzhengjie.org.cn;

    location / {
        root /yinzhengjie/data/web/nginx/html/mobile;
        index index.html;
    }

}
[[email protected] ~]# 
[[email protected] ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[[email protected] ~]#

3>.创建测试数据

[[email protected] ~]# mkdir -pv /yinzhengjie/data/web/nginx/html/{pc,mobile}
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html/pc’
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html/mobile’
[[email protected] ~]# 
[[email protected] ~]# echo "

尹正杰到此一游

" > /yinzhengjie/data/web/nginx/html/pc/index.html [[email protected] ~]# [[email protected] ~]# echo "

Jason's mobile email:y1053419035

" > /yinzhengjie/data/web/nginx/html/mobile/index.html [[email protected] ~]#

4>.启动nginx服务

[[email protected] ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[[email protected] ~]# 
[[email protected] ~]# nginx 
[[email protected] ~]# 
[[email protected] ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:80                                                                     *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[[email protected] ~]# 
[[email protected] ~]# ps -ef | grep nginx | grep -v grep
root      2999     1  0 22:24 ?        00:00:00 nginx: master process nginx
nginx     3000  2999  1 22:24 ?        00:00:00 nginx: worker process
nginx     3001  2999  1 22:24 ?        00:00:00 nginx: worker process
nginx     3002  2999  1 22:24 ?        00:00:00 nginx: worker process
nginx     3003  2999  1 22:24 ?        00:00:00 nginx: worker process
[[email protected] ~]# 
[[email protected] ~]# 

 

三.测试

1>.浏览器访问“http://pc.yinzhengjie.org.cn/”,如下图所示。

2>.浏览器访问"http://mobile.yinzhengjie.org.cn/",如下图所示。

3>.浏览器访问"http://mail.yinzhengjie.org.cn/",如下图所示。

 

你可能感兴趣的:(Nginx 核心配置-单节点实现多域名访问)