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

                                       作者:尹正杰

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

 

一.试验环境说明

1>.虚拟机环境说明

[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.-.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. (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 ;
worker_cpu_affinity ; events {
worker_connections ;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-;
keepalive_timeout ;
server {
listen ;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page /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 ;
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 ;
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 *: *:*
LISTEN ::: :::*
[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 *: *:*
LISTEN *: *:*
LISTEN ::: :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: 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/",如下图所示。

Nginx 核心配置-单节点实现多域名访问的更多相关文章

  1. Nginx 核心配置-作为上传服务器配置

    Nginx 核心配置-作为上传服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.关键参数说明 client_max_body_size 1m: 设置允许客户端上传单 ...

  2. Nginx 核心配置-可优化配置参数

    Nginx 核心配置-可优化配置参数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.nginx的官网查看指令帮助信息方法 1>.打开nginx的官网(https://ng ...

  3. Nginx 核心配置-作为下载服务器配置

    Nginx 核心配置-作为下载服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.无限速版本的下载服务器 1>.查看主配置文件 [root@node101.yinz ...

  4. Nginx 核心配置-长连接配置

    Nginx 核心配置-长连接配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.长连接配置参数说明 keepalive_timeout number; 设定保持连接超时时长,0 ...

  5. Nginx 核心配置-location的登录账户认证实战篇

    Nginx 核心配置-location的登录账户认证实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用ab命令模拟网站攻击 1>.安装httpd-tools工具 ...

  6. Nginx 核心配置-location的匹配案例实战篇

    Nginx 核心配置-location的匹配案例实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.location语法规则介绍 在没有使用正则表达式的时候,nginx会先在 ...

  7. Nginx 核心配置-检测文件是否存在

    Nginx 核心配置-检测文件是否存在 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件 ...

  8. Nginx 核心配置-自定义日志路径及清空日志注意事项

    Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...

  9. Nginx 核心配置-自定义错误页面

    Nginx 核心配置-自定义错误页面 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 生产环境中错误页面一般都是UI或开发工程师提供的,他们已经在软件中定义好了,我们这里就简单写个h ...

随机推荐

  1. File.createNewFile和 File.createTempFile比较和区别

    原文地址:http://wzhiju.iteye.com/blog/1119037 最近,在看代码时看到了一个方法, File.createTempFile() ,由此联想到File.createNe ...

  2. 数据结构——栈与递归(recursion)

    /* recursion.c */ /* 递归 */ #include <stdio.h> void interface(void); /* 斐波那契数列以及阶乘函数声明 */ long ...

  3. [LeetCode] 63. Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. oracle--delete truncate drop的区别

    一.delete 1.delete是DML,执行delete操作时,每次从表中删除一行,并且同时将该行的的删除操作记录在redo和undo表空间中以便进行回滚(rollback)和重做操作,但要注意表 ...

  5. java web开发入门七(mybatis)基于intellig idea

    mybatis 一. 入门开发步骤 1.导入相关jar包 mybatis3.4.2核心开发包 asm-5.1.jarcglib-3.2.4.jarcommons-logging-1.1.3.jarlo ...

  6. 吉特仓储管理系统-ERP或WMS系统中常见术语

    MPS---主生产计划(Master Production schedules) MTO---订货生产(Make-to-Order) BOM---物料清单或产品结构表(Bill of material ...

  7. k8s集群-node节点设置不可调度或者删除node节点

    在master 执行1, 不可调度 kubectl cordon k8s-node- kubectl uncordon k8s-node- #取消 2,驱逐已经运行的业务容器 kubectl drai ...

  8. Where are registered servers stored?

    https://stackoverflow.com/questions/3064289/where-are-registered-servers-stored   They are kept as a ...

  9. 【转】PyQt弹出式对话框的常用方法及标准按钮类型

    pyQt之弹出式对话框(QMessageBox)的常用方法及标准按钮类型 一.控件说明 QMessageBox是一种通用的弹出式对话框,用于显示消息,允许用户通过单击不同的标准按钮对消息进行反馈,且每 ...

  10. Vue.js+vue-element

    Vue.js+vue-element搭建属于自己的后台管理模板:什么是Vue.js?(一)   Vue.js+vue-element搭建属于自己的后台管理模板:Vue.js是什么?(一) 前言 本教程 ...