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

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

                                       作者:尹正杰

 

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

 

一.试验环境说明

1>.虚拟机环境说明

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

 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的主配置文件

[root@node101.yinzhengjie.org.cn ~]# 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;
}

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 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
[root@node101.yinzhengjie.org.cn ~]# 

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

[root@node101.yinzhengjie.org.cn ~]# 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;
    }

}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 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;
    }

}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 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
[root@node101.yinzhengjie.org.cn ~]#

3>.创建测试数据

[root@node101.yinzhengjie.org.cn ~]# 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’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(255,0,0)'>尹正杰到此一游</h1>" > /yinzhengjie/data/web/nginx/html/pc/index.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(0,0,255)'>Jason's mobile email:y1053419035</h1>" > /yinzhengjie/data/web/nginx/html/mobile/index.html
[root@node101.yinzhengjie.org.cn ~]# 

4>.启动nginx服务

[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 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                                                                    :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 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
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

三.测试

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

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

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

原文地址:https://www.cnblogs.com/yinzhengjie/p/12032691.html