Nginx网站服务详解(一)
通过CentOS7系统环境下进行演示
Nginx部署环境准备
1、yum环境部署
使用阿里云镜像源
配置方法:
a、备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
b、下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
c、运行 yum makecache 生成缓存
2、使用清华镜像源
写入清华镜像站 Centos 的内容:
cat > /etc/yum.repos.d/centos.repo << EOF[base]name=CentOS-\$releasever - Basebaseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#released updates[updates]name=CentOS-\$releasever - Updatesbaseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/updates/\$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#additional packages that may be useful[extras]name=CentOS-\$releasever - Extrasbaseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#additional packages that extend functionality of existing packages[centosplus]name=CentOS-\$releasever - Plusbaseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/centosplus/\$basearch/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7EOF
其余步骤与上述阿里云部署一致
安装 epel 源
yum install epel-release -y
写入清华镜像站 epel 的内容:
cat > /etc/yum.repos.d/epel.repo << EOF[epel]name=Extra Packages for Enterprise Linux 7 - \$basearchbaseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/\$basearchfailovermethod=priorityenabled=1gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7EOF
2、准备好hosts文件和hostname
因为搭建的使用的是本地网络进行访问,所以这里要准备好hostname和hosts文件的标准化,有助于我们学习一系列课程的便捷和减少遇到一些不必要的麻烦。
hostname建议使用-进行分割命名含义,不要使用下划线_。这是使用DNS对下划线 _不能解析。
hosts文件是DNS解析文件。
我机器配置参考:
hostname文件
[root@liu-node1 ~]# cat /etc/hostnameliu-node1
hosts文件
测试机有两块网卡
[root@liu-node1 ~]# cat /etc/hosts192.168.0.44 liu-node2 liu-node2.liu.com192.168.0.43 liu-node1 liu-node1.liu.com192.168.0.43 www.liuluanyi.cn10.0.0.43 www.liuluanyi.cn192.168.0.43 bbs.liuluanyi.cn192.168.0.43 blog.liuluanyi.cn
3、yum安装Nginx服务
yum install nginx -y
4、查看Nginx版本
[root@liu-node1 ~]# nginx -vnginx version: nginx/1.16.1
5、关闭selinux 这个可以减少初学者许多不必要的麻烦,后面学习的深入建议开启
[root@liu-node1 ~]# cat /etc/selinux/config
效果如下:
# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=disabled# SELINUXTYPE= can take one of three values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes are protected.# mls - Multi Level Security protection.SELINUXTYPE=targeted
6、查看Nginx配置文件
[root@liu-node1 ~]# cat /etc/nginx/nginx.conf
修改成如下配置:
user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;#include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;}http {log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;include /etc/nginx/mime.types; //媒体资源类型文件作用default_type application/octet-stream;include /etc/nginx/conf.d/*.conf; //加载多个虚拟主机文件
Nginx配置文件参数解释
1、log日志参数
访问日志: /var/log/nginx/access.log ngx_http_log_modulelog_format main '$remote_addr - $remote_user [$time_local] "$request" ' 定义日志内容格式'$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main; 调用日志格式$remote_addr 显示用户访问源IP地址信息$remote_user 显示认证的用户名信息[$time_local] 显示访问网站时间"$request" 请求报文的请求行信息$status 用户访问网站状态码信息$body_bytes_sent 显示响应的数据尺寸信息$http_referer 记录调用网站资源的连接地址信息(防止用户盗链)$http_user_agent 记录用户使用什么客户端软件进行访问页面的$http_x_forwarded_for ??? 负载均衡
2、nginx常用命令参数
-t : test configuration and exit检查测试配置文件语法-s : send signal to a master process: stop, quit, reopen, reload控制服务停止或者重新启动
3、虚拟主机配置文件解释
server {listen 80; //监听端口server_name www.liuluanyi.cn; 别名;访问域名location / { //location指定网站资源root /html/www; //指定网站目录index index.html; //首页文件}}
Nginx多站点搭建
1、创建站点目录和目录中首页文件
[root@liu-node1 conf.d]# mkdir /html/{www,bbs,blog} -p[root@liu-node1 conf.d]# for name in {www,bbs,blog};do echo "192.168.0.43 $name.liuluanyi.cn" >/html/$name/index.html ;done
查看配置结果:
[root@liu-node1 conf.d]# for name in {www,bbs,blog};do cat /html/$name/index.html ;done192.168.0.43 www.liuluanyi.cn192.168.0.43 bbs.liuluanyi.cn192.168.0.43 blog.liuluanyi.cn
2、创建多个虚拟主机配置文件
bbs.conf
server {listen 80;server_name bbs.liuluanyi.cn;location / {root /html/bbs;index index.html;}}
blog.conf
server {listen 80;server_name blog.liuluanyi.cn;location / {root /html/blog;index index.html;}}
www.conf
server {listen 80;server_name www.liuluanyi.cn;location / {root /html/www;index index.html;}}
==nginx配置文件修改一定要重启服务==
systemctl reload nginx //平滑重启systemctl restart nginx //硬式重启
3、访问测试
[root@liu-node1 conf.d]# curl www.liuluanyi.cn192.168.0.43 www.liuluanyi.cn[root@liu-node1 conf.d]# curl blog.liuluanyi.cn192.168.0.43 blog.liuluanyi.cn[root@liu-node1 conf.d]# curl bbs.liuluanyi.cn192.168.0.43 bbs.liuluanyi.cn
涉及多个站点,如果我直接通过192.168.0.43默认访问www.liuluanyi.cn的站点,可以进行下面操作:
编辑nginx配置文件/etc/nginx/nginx.conf
include /etc/nginx/conf.d/www.conf; //添加的配置include /etc/nginx/conf.d/*.conf;
Nginx重启后,进行验证:
[root@liu-node1 conf.d]# curl 192.168.0.43192.168.0.43 www.liuluanyi.cn
4、虚拟主机访问方式
a、基于域名的方式进行访问:
负载均衡+高可用服务 场景使用较多
server {listen 192.168.0.43:80;server_name www.liuluanyi.cn;location / {root /html/www;index index.html;}}
平滑重启的效果:
[root@liu-node1 conf.d]# netstat -ntlp | grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6458/nginx: master
硬式重启的效果:
[root@liu-node1 conf.d]# netstat -ntlp | grep 80tcp 0 0 192.168.0.43:80 0.0.0.0:* LISTEN 29532/nginx: master
c、基于端口的方式进行访问:
zabbix服务(apache:80) + web服务(nginx:80) --> 主机
多个服务部署到同一个主机上
server {listen 8080;server_name www.liuluanyi.cn;location / {root /html/www;index index.html;}}
Nginx网站的安全访问配置
10.0.0.0/24 www.liuluanyi.cn/file/ 不能访问192.168.0.0/24 www.liuluanyi.cn/file/ 可以访问
创建相应的访问文件
[root@liu-node1 conf.d]# mkdir -p /html/www/file[root@liu-node1 conf.d]#[root@liu-node1 conf.d]# echo "liu-node1" > /html/www/file/index.html
编辑www.conf站点配置文件
server {listen 80;server_name www.liuluanyi.cn;location / {root /html/www;index index.html;}location /file {root /html/www;index index.html;}
放在location之外的代表全局变量,location之内的代表内部变量,进行测试访问。
确定是在192.168.0.0/24网段访问
[root@liu-node1 conf.d]# ping www.liuluanyi.cnPING www.liuluanyi.cn (192.168.0.43) 56(84) bytes of data.64 bytes from liu-node1 (192.168.0.43): icmp_seq=1 ttl=64 time=0.070 ms64 bytes from liu-node1 (192.168.0.43): icmp_seq=2 ttl=64 time=0.096 ms
curl访问测试
[root@liu-node1 conf.d]# curl www.liuluanyi.cn/file/index.htmlliu-node1
确定是在10.0.0.0/24网段访问
[root@liu-node1 conf.d]# ping www.liuluanyi.cPING www.liuluanyi.cn (10.0.0.43) 56(84) bytes of data.64 bytes from liu-node1 (10.0.0.43): icmp_seq=1 ttl=64 time=0.070 ms64 bytes from liu-node1 (10.0.0.43): icmp_seq=2 ttl=64 time=0.096 ms
curl访问测试
[root@liu-node1 conf.d]# curl www.liuluanyi.cn/file/index.htmlliu-node1
访问nginx官网查看nginx访问模块: ngx_http_access_module的使用方法。
官网举例配置:
location / {deny 192.168.1.1;allow 192.168.1.0/24;allow 10.1.1.0/16;allow 2001:0db8::/32;deny all;}
配置指令的使用方法
Syntax: allow address | CIDR | unix: | all;Default: —Context: http, server, location, limit_except
最终修改成下所示:
server {listen 80;server_name www.liuluanyi.cn;location / {root /html/www;index index.html;}location /file {deny 10.0.0.0/24;allow 192.168.0.0/24;root /html/www;index index.html;}
b、根据用户访问进行认证
nginx认证模块: ngx_http_auth_basic_module
官网配置举例:
location / {auth_basic "closed site"; --- 开启认证功能auth_basic_user_file conf/htpasswd; --- 加载用户密码文件}
编写虚拟主机配置文件
server {listen 80;server_name www.liuluanyi.cn;location / {root /html/www;index index.html;auth_basic "liu-node1";auth_basic_user_file password/htpasswd;}
