服务-WEB-Nginx

Nginx-静态Webserver

Nginx 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。
HTTP web
反向代理 squid
IMAP 互联网消息访问协议,收邮件,tcp/143
POP3 邮局通讯协议第三版,收邮件,tcp/110
SMTP 简单邮件传输协议,发邮件,tcp/25

优点:
稳定性、丰富的功能集、示例配置文件和低系统资源的消耗

1. 开始安装

下载包
官网:http://nginx.org/
[root@nginx1 ~]# lftp 172.16.0.99
lftp 172.16.0.99:~> cd scripts/
lftp 172.16.0.99:/scripts> get nginx-1.13-clean.sh 

[root@nginx1 ~]# chmod +x nginx-1.13-clean.sh
[root@nginx1 ~]# ./nginx-1.13-clean.sh 

[root@nginx1 ~]# cd /usr/local/nginx/
[root@nginx1 /usr/local/nginx]# ls

2. 讲解配置文件

注意分号,和括号

[root@nginx1 /usr/local/nginx/conf]# vim nginx.conf
##全局模块
user  www;		运行nginx进程的用户
worker_processes  1;
	开启的进程数
	与CPU的线程数是一致的
----------->[root@nginx1 ~]# grep -c "proc" /proc/cpuinfo 
				1	查看CPU的线程数

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;
	每个进程能够处理的最大连接数
	nginx能够处理的最大连接数=worker_processes x worker_connections
    use epoll;
	高效的网络IO模型
	支持多路复用,SMP
}

##HTTP模块
http {
    include       mime.types;
	支持的文件扩展名和文件类型的映射文件
    default_type  application/octet-stream;
	数据传输模式,二进制流
    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  logs/access.log  main;
	访问日志的格式和路径
    sendfile       on;
	开启高效的文件传输模式
    tcp_nopush     on;
	防止网络阻塞
    keepalive_timeout  65;
	长连接的超时时间

	
--------------------------------
长连接:在一个连接上,可以连续发送多个数据包,然后再断开连接;
	在连接保持期间,如果没有数据发送,双方要发送链路检测的数据包。
短连接:双方在数据传输时,只建立一个连接,发送完成后,立即断开。
	每次连接只完成一项数据发送。
--------------------------------
    gzip  on;
	开启传输压缩
##虚拟主机
    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;
        }
    }
}
     
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	检查配置文件的语法
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload
	重新加载

Nginx的虚拟主机:

域名:
	www1.nginx.com
	www2.nginx.com
访问路径:
	/datanginx/www1
	/datanginx/www2
   server {
        listen       80;
        server_name  www1.nginx.com;
        location / {
            root   /datanginx/www1;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  www2.nginx.com;
        location / {
            root   /datanginx/www2;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80 default;
        location / {
            root   /usr/local/nginx/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
       }
   }

创建访问路径

[root@nginx1 ~]# mkdir -p /datanginx/www{1,2}
[root@nginx1 ~]# cd /datanginx/
[root@nginx1 /datanginx]# ls
www1  www2

设置页面首页

[root@nginx1 /datanginx]# echo "www1.nginx.com" > www1/index.html
[root@nginx1 /datanginx]# echo "www2.nginx.com" > www2/index.html

[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

访问测试:
linux 客户端 (hosts文件进行ip绑定,绑定nginx服务端ip)

[root@shell ~]# tail -2 /etc/hosts
172.16.0.31	www1.nginx.com
172.16.0.31	www2.nginx.com

[root@shell ~]# curl www1.nginx.com
www1.nginx.com
[root@shell ~]# curl www2.nginx.com
www2.nginx.com
[root@shell ~]# curl 172.16.0.31
	默认测试页

windows 客户端:
(hosts文件进行ip绑定,绑定nginx服务端ip)

C:\Windows\System32\drivers\etc
	hosts
		172.16.0.31	www1.nginx.com
		172.16.0.31	www2.nginx.com

Nginx的日志管理:

引用

[root@nginx1 /usr/local/nginx/conf]# vim nginx.conf    部分内容
	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  logs/access.log  main;

日志 的解释:

log_format 定义了日志记录的内容
$remote_addr 客户端的IP
$remote_user 客户端的用户
$time_local 访问时间和时区
$request 请求的协议
$status 状态码
$body_bytes_sent 传输给客户端的数据大小
$http_referer 从哪个页面链接过来的
$http_user_agent 浏览器的信息
$http_x_forwarded_for 记录客户端的真实地址

日志的语句示例
# head -1 access.log
172.16.0.20 - - [22/Nov/2019:15:57:22 +0800] “GET / HTTP/1.1” 200 396 “-” “Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0” “-”

客户端IP:记录客户端来源
访问时间:判断出访问时间的波峰和波谷
状态码 200
1xx 消息
2xx 成功 200
3xx 重定向
4xx 客户端请求错误
5xx 服务器自身错误
浏览器信息:

access_log 定义了日志的保存路径
main 名字

日志轮滚:
rpm包安装的,自带轮滚脚本。
编译安装的,自己写
配置日志轮滚,/etc/logrtate.d/目录下自定义添加要轮滚的日志配置文件,路径,参数等。

[root@nginx1 /etc/logrotate.d]# vim nginx   # 创建编辑nginx的轮滚日志配置文件
/usr/local/nginx/logs/access.log
/usr/local/nginx/logs/error.log
{
    daily			# 每天
    create			# 创建
    missingok		# 忽略错误
    notifempty		# 当日志文件为空时,不进行轮转
    dateext			# 日志文件末尾加日期
    rotate 7		# 轮滚保留7份
    create 0600 root root  # 创建日志文件的权限
    sharedscripts	
        postrotate
    	/usr/bin/kill -HUP `cat /usr/local/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true
	endscript
}

测试:

[root@nginx1 /etc/logrotate.d]# logrotate -f /etc/logrotate.conf 

[root@nginx1 /usr/local/nginx/logs]# ls
access.log  access.log-20191123  error.log  error.log-20191123  nginx.pid

为什么日志轮滚以后,要执行该操作?
日志轮滚后,原日志文件被改名,但是inode不变;
新文件被创建,但是原进程仍然识别旧的日志文件的inode,不会向新日志文件写入
重新加载配置文件,让新日志文件生效。

日志轮滚操作,由 谁 控制,什么时候执行?
计划任务

[root@nginx1 /etc/cron.daily]# cat logrotate 
#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

[root@nginx1 /etc/cron.daily]# tail -3 /etc/anacrontab 
1	5	cron.daily		nice run-parts /etc/cron.daily

百度 /etc/anacrontab 了解


访问控制:
约束哪些用户可以访问到nginx。

[root@nginx1 /usr/local/nginx]# vim conf/nginx.conf

    server {
        listen       80 default;
        location / {
            root   /usr/local/nginx/html;
            index  index.html index.htm;
加          auth_basic  "test auth";
加          auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

[root@nginx1 ~]# yum install -y httpd

[root@nginx1 ~]# htpasswd -c /usr/local/nginx/conf/.htpasswd nginx1
New password: 
Re-type new password: 
Adding password for user nginx1

[root@nginx1 ~]# cat /usr/local/nginx/conf/.htpasswd 
nginx1:$apr1$PaqdcFSE$NIYH4xRDf68kxS1mYFrDQ0

[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

访问:http://172.16.0.31/
用户名:nginx1
密码:自定义的

基于IP的:

    server {
        listen       80 default;
        location / {
            root   /usr/local/nginx/html;
            index  index.html index.htm;
#           auth_basic  "test auth";
#           auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
        
            allow   172.16.0.20;	# 允许这个ip访问
            deny all;				# 其它都拒绝

        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

测试:
只有172.16.0.20可以访问。
[root@nginx1 /usr/local/nginx]# curl 172.16.0.31

403 Forbidden 其他主机,没有权限

白名单:
允许局部,拒绝所有
allow ip;
allow ip;
deny all;

黑名单:
拒绝局部,允许所有
deny ip;
deny ip;

如何判定应该用黑名单还是白名单?

网站前台:黑名单
	通过分析访问日志,找出恶意访问的IP,拒绝
网站后台:白名单
	只允许公司的公网IP访问

Nginx的rewrite(重定向)

域名重定向。
www.a.com --> www.b.com

应用:
官方网站
多个网站地址,–> 一个网站首页。

正则表达式!!!

准备页面:

[root@nginx1 /datanginx/www1]# mkdir a b
[root@nginx1 /datanginx/www1]# echo aaaaaa > a/index.html
[root@nginx1 /datanginx/www1]# echo bbbbbbbb > b/index.html

目的: 将访问a的结果,重定向到b里面:

设置hosts文件
[root@nginx1 /usr/local/nginx]# tail -1 /etc/hosts
172.16.0.31	www1.nginx.com

[root@nginx1 /usr/local/nginx]# vim conf/nginx.conf
        location /a/ {
            proxy_pass http://www1.nginx.com/b/;
            rewrite "^/a/(.*)\.html" /datanginx/www1/b/index.html;
            表示将所有a的.HTML访问,   转给b/的.html访问
        }
			下边这两行是已经存在的,属于基本设置,在重定向的括号外边
            root   /datanginx/www1;
            index  index.html index.htm;

配置完成后重新启动
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t  
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

测试:
访问测试:http://www1.nginx.com/a/
得到的结果是b的测试页。


实验2
准备页面:

[root@nginx1 /datanginx/www1]# echo 123 > 123.html
[root@nginx1 /datanginx/www1]# echo 456 > 456.html
[root@nginx1 /datanginx/www1]# cd ..
[root@nginx1 /datanginx]# echo 789 > www2/789.html

目的: 将访问123.html测试页,定向到456.html测试页

        location ~ 123\.html {
            rewrite ^/123\.html$ http://www1.nginx.com/456.html;
        }
			将访问123.html测试页,定向到456.html测试页
			
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

访问测试:www1.nginx.com/123.html
	得到456的测试页内容

        location ~ [0-9]+\.html {
            rewrite ^/[0-9]+\.html$ http://www1.nginx.com/a/;
        }

[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload



访问测试:
	www1.nginx.com/123.html
	www1.nginx.com/456.html
	得到 www1.nginx.com/a/index.html

最后将全部的页面都指向一个地址
location ~ [a-z0-9]+\.html {
    rewrite [a-z0-9]+\.html http://www2.nginx.com/;
}


[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

访问测试:
	www1.nginx.com	所有的访问,都被重定向到 www2.nginx.com

nginx的负载均衡和反向代理 ★★★★★

重点

172.16.0.101 apache1
172.16.0.102 apache2
172.16.0.103 apache3
172.16.0.104 apache4

负载均衡:
nginx软件七层负载均衡。
使用反向代理

nginx反向代理
client <-----nginx------> server
客户端向server索取数据。

squid和nginx的不同之处:
squid同步模式
nginx异步模式
client上传数据


先在每台设备上写入测试页,然后启动apache服务
用nginx服务器访问每台设备,确认启动状态

[root@nginx1 ~]# curl 172.16.0.101
apache1
[root@nginx1 ~]# curl 172.16.0.102
apache2
[root@nginx1 ~]# curl 172.16.0.103
apache3
[root@nginx1 ~]# curl 172.16.0.104
apache4

轮询

编辑配置文件:(轮询)

[root@nginx1 /usr/local/nginx]# vim conf/nginx.conf
    upstream webA {
        server 172.16.0.101;
        server 172.16.0.102;
    }
    server {
        listen       80;
        server_name  www1.nginx.com;
        location / {
            proxy_pass http://webA;
            proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
            include /usr/local/nginx/conf/proxy.conf;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

[root@nginx1 /usr/local/nginx]# vim conf/proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload


访问测试:
[root@centos7-bj ~]# curl www1.nginx.com
apache1
[root@centos7-bj ~]# curl www1.nginx.com
apache2
[root@centos7-bj ~]# curl www1.nginx.com
apache1
[root@centos7-bj ~]# curl www1.nginx.com
apache2

当前的部署方式,是轮询。


轮询+权重

配置带有 权重+轮询
权重:取决于服务器的硬件性能,性能高,权重值大;
权重最小值 1 ,上限 无 ,按照比例进行分配访问请求
权重是正整数,尽量不要用1

    upstream webA {
        server 172.16.0.101 weight=10;
        server 172.16.0.102 weight=20;
    }

[root@nginx1 /usr/local/nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 /usr/local/nginx]# ./sbin/nginx -s reload

[root@centos7-bj ~]# curl www1.nginx.com
apache1
[root@centos7-bj ~]# curl www1.nginx.com
apache2
[root@centos7-bj ~]# curl www1.nginx.com
apache2

IP_HASH

当第一次访问到服务器,会跟服务器进行绑定

ip_hash	客户端绑定
upstream webA {
    ip_hash;
    server 172.16.0.101 weight=10;
    server 172.16.0.102 weight=20;
}

故障检测

添加故障检测后,当正在使用的服务器出现问题,会自动切换到另一台服务器。

upstream webA {
    ip_hash;				添加在服务器的ip末尾;
    server 172.16.0.101 weight=10 max_fails=2 fail_timeout=3s;
    server 172.16.0.102 weight=20 max_fails=2 fail_timeout=3s;
	检测的次数,检测的间隔时间
}

htpasswd -c 不要乱用

show variables like ‘% %’
错误日志 默认开启

Navicat for MySQL

-exec 非交互式的 直接执行命令
find -type f 查找文件类型是普通文件的
-mtime +7 查找7天以前的文件

vim 定位行 命令行输入行号 即可

参考

复习:
http:超文本传输协议
web-server的传输协议
html:超文本标记语言
静态页面
URL:统一资源定位符
浏览器地址栏
www:万维网
访问方式

web服务器的功能?
发布信息,提供网页服务

client 如何 访问 web-server?
浏览器输入域名
DNS解析
定位到web-server的IP
分析包头 header,看请求的具体内容
服务器根据请求返回


apache的工作模式:

  1. 进程模式
    一个子进程,产生一个线程,响应一个客户端,响应完成以后,资源回收,子进程会被“杀死”
    默认开启5个子进程

  2. 线程模式
    一个子进程,产生若干线程,每个线程响应一个客户端,响应完成以后,回收线程

  3. 事件模式

https://blog.51cto.com/balich/1743798


发布了57 篇原创文章 · 获赞 3 · 访问量 1020

猜你喜欢

转载自blog.csdn.net/weixin_42502744/article/details/103204737
今日推荐