@nginx常用初始内容
文章目录
一、高可用的概述
1.什么是高可用
2.keepalived 如何实现的高可用
keepalived软件是基于VRRP协议实现的IP漂移,解决单点故障
3.VRRP协议是什么
在服务器前端调用VRRP协议,创建一个VIP和VMAC,VIP进行漂移,VMAC地址通知ARP广播修改缓存表内容
4.高可用的核心概念
1.如何确定主备节点(设置优先级)
2.如果maser故障,backup自动切换,如果这个时候master恢复后,会不会自动切换回来(抢占式和非抢占式)
3.两台服务器都认为自己是主节点会怎么样(脑裂)
5.安装keepalived
1)下载安装keepalived
yum install -y keepalived
2)配置keepalived
#主节点配置
global_defs { #全局
router_id lb01 #身份认证(唯一的)
}
vrrp_script check_web { #定义脚本
script "/root/check_web.sh" #脚本文件路径
interval 5 #脚本执行时间间隔
}
vrrp_instance VI_1 {
state MASTER #节点状态 主节点
interface eth0 #绑定网卡,监听地址
virtual_router_id 51 #组ID
priority 100 #优先级
advert_int 1 #监听时间间隔
authentication { #认证方式
auth_type PASS #密码的方式
auth_pass 1111 #密码是多少
}
virtual_ipaddress {
10.0.0.3 #虚拟VIP
}
track_script { #调用脚本
check_web #脚本名称
}
}
#备节点配置
global_defs { #全局
router_id lb02 #身份认证(唯一的)
}
vrrp_instance VI_1 {
state BACKUP #节点状态 备节点
interface eth0 #绑定网卡,监听地址
virtual_router_id 51 #组ID
priority 90 #优先级
advert_int 1 #监听时间间隔
authentication { #认证方式
auth_type PASS #密码的方式
auth_pass 1111 #密码是多少
}
virtual_ipaddress {
10.0.0.3 #虚拟VIP
}
}
#注意:
如果配置抢占式,脚本只需要在主节点配置
如果配置为非抢占式,脚本需要再主备节点都配置
3)启动keepalived
systemctl start keepalived
6.keepalived抢占式和非抢占式
1)抢占式
#keepalived默认是抢占式,当主节点关闭keepalived,从节点自动接管VIP,当主节点恢复,则会把VIP强行抢占回去
2)非抢占式
1.两个节点都要配置成BACKUP
2.添加配置nopreempt
3.优先级一定保持不同
#主节点配置
vrrp_instance VI_1 {
state BACKUP #节点状态 主节点
interface eth0
nopreempt
priority 100
}
#主节点配置
vrrp_instance VI_1 {
state BACKUP #节点状态 备节点
interface eth0
nopreempt
priority 90
}
7.脑裂
1)脑裂的原因
1.网络,网线松动
2.硬件,服务器硬件损坏
3.防火墙开启
2)解决脑裂方法
干掉一台keepalived
8.keepalived和nginx
Nginx常见问题
一、nginx多server优先级
在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中的每个server_name进行匹配,以此决定到底由哪一个server来处理这个请求,但nginx如何配置多个相同的server_name,会导致server_name出现优先级访问冲突。
1.准备多个配置文件
[root@web01 conf.d]# cat server1.conf
server {
listen 80;
server_name localhost test1.com;
location / {
root /code/test1;
index index.html;
}
}
[root@web01 conf.d]# cat server2.conf
server {
listen 80;
server_name localhost test2.com;
location / {
root /code/test2;
index index.html;
}
}
[root@web01 conf.d]# cat server3.conf
server {
listen 80;
server_name localhost test3.com;
location / {
root /code/test3;
index index.html;
}
}
You have new mail in /var/spool/mail/root
[root@web01 conf.d]#
2.配置站点文件
[root@web01 conf.d]# mkdir /code/test{1..3}
You have new mail in /var/spool/mail/root
[root@web01 conf.d]# echo "test1" > /code/test1/index.html
[root@web01 conf.d]# echo "test2" > /code/test2/index.html
[root@web01 conf.d]# echo "test3" > /code/test3/index.html
[root@web01 conf.d]# chown -R www.www /code/
3.启动nginx
[root@web01 conf.d]# nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfu
#讲稿提醒有同样的配置内容
[root@web01 conf.d]# systemctl restart nginx
4.配置hosts访问测试
10.0.0.7 test1.com test2.com test3.com
5.多server优先级总结
1.首先选择所有的字符串完全匹配的server_name。(完全匹配)
2.选择通配符在前面的server_name,如*.mumusir.com www.mumusir.com
3.选择通配符在后面的server_name,如mumusir.* mumusir.com mumusir.cn
4.最后选择使用正则表达式匹配的server_name,如~^www\.(.*)\.com$
5.如果全部都没有匹配到,那么将选择在listen配置项后加入[default_server]的server块
6.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件
6.总结测试
[root@web01 conf.d]# cat server1.conf
server {
listen 80;
server_name localhost www.test.com;
location / {
root /code/test1;
index index.html;
}
}
[root@web01 conf.d]# cat server2.conf
server {
listen 80;
server_name localhost *.test.com;
location / {
root /code/test2;
index index.html;
}
}
[root@web01 conf.d]# cat server3.conf
server {
listen 80;
server_name localhost www.test.*;
location / {
root /code/test3;
index index.html;
}
}
[root@web01 conf.d]# cat server4.conf
server {
listen 80;
server_name localhost ~^www\.(.*)\.com$;
location / {
root /code/test3;
index index.html;
}
}
[root@web01 conf.d]#
二、nginx禁止IP访问网站
1.禁止ip访问直接返回错误
[root@web01 conf.d]# vim server4.conf
server {
listen 80 default_server;
server_name localhost;
return 500;
}
2.引流的方式,访问IP跳转到主页
server {
listen 80 default_server;
server_name localhost;
return 302 https://www.baidu.com;
}
3.返回指定的内容
server {
listen 80 default_server;
server_name localhost;
default_type text/plain;
return 200 "页面错误.......";
}
三、nginx的包含 include
一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。
假设现在希望快速的关闭一个站点,该怎么办?
1.如果是写在nginx.conf中,则需要手动注释,比较麻烦
2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释 Include包含的作用是为了简化主配置文件,便于人类可读。
3.两个目录
include /etc/nginx/online/*.conf #线上使用
mv .cnof /etc/nginx/offlie/ #下线的配置,临时撤站
四、nginx路径的 root与alias
root与alias路径匹配主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上,alias是一个目录别名的定义,root则是最上层目录的定义。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias定义的路径
1.root和alias配置
[root@web01 conf.d]# vim image.conf
server {
listen 80;
server_name image.com;
location /picture {
root /code;
}
}
#使用root的时候,访问http://image.com/picture/1.gif,实际上会到服务器的/code/picture/目录下面寻找1.gif文件
[root@web01 conf.d]# vim image.conf
server {
listen 80;
server_name image.com;
location /picture {
alias /code;
}
}
#如果使用的是alias,访问http://image.com/picture/1.gif,实际上是到服务器的/code/目录下寻找1.gif文件
2.生产中配置方式
server {
listen 80;
server_name image.com;
location / {
root /code;
index index.html 1.html test.html
}
location ~* \.(jpg|png|gif)$ {
alias /code/images;
}
}
五、Nginx的 try_file路径匹配
nginx的try_file路径匹配,Nginx会按顺序检查文件及目录是否存在(根据 root 和 alias 指令设置的参数构造完整的文件路径),并用找到的第一个文件提供服务。在元素名后面添加斜杠 / 表示这个是目录。如果文件和目录都不存在,Nginx会执行内部重定向,跳转到命令的最后一个 uri 参数定义的 URI 中。
1.配置try_files
#配置nginx
[root@web01 conf.d]# vim try.conf
server {
listen 80;
server_name try.com;
location / {
root /code/try;
try_files $uri /404.html;
}
}
#创建目录与文件
[root@web01 conf.d]# mkdir /code/try
[root@web01 conf.d]# echo "try_file.index" > /code/try/index.html
[root@web01 conf.d]# echo 404040404 > /code/try/404.html
#访问try.com,匹配$uri,而域名后面为空,匹配不到内容,所以匹配/404.html,返回的内容是/code/try/404.html
[root@web02 ~]# curl try.com
404040404
#访问try.com/index.html,匹配$uri匹配到了/index.html,返回的内容就是/code/try/index.html
[root@web02 ~]# curl try.com/index.html
try_file.index
#修改nginx配置为
[root@web01 conf.d]# vim try.conf
server {
listen 80;
server_name try.com;
location / {
root /code/try;
try_files $uri $uri/ /404.html;
}
}
#访问try.com,$uri匹配不到内容,交给$uri/匹配,匹配到的是 “空/”,所以访问的链接是 try.com/ ,则能访问到/code/try/index.html
2.实例配置
#1. 配置nginx
[root@lb01 conf.d]# cat try.conf
server {
listen 80;
server_name try.com;
root /code;
location / {
try_files $uri $uri/ @java; #当$uri和$uri/都匹配不到时,由后端的java来进行处理,名字可自定义,但一定要加@,内部子请求
}
location @java {
proxy_pass http://172.16.1.8:8080; #配置后端tomcat
}
}
六、Nginx 调整上传文件大小
1.nginx 上传文件大小限制语法
Syntax: client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location
2.nginx上传文件大小限制配置
http {
....
client_max_body_size 200m;
....
}
七、Nginx优雅显示错误页面
1.跳转到网上
server {
listen 80;
server_name error.linux.com;
location / {
root /code;
index index.html;
error_page 404 http://www.baidu.com;
}
}
2.跳转到本地文件
server {
listen 80;
server_name error.linux.com;
location / {
root /code;
index index.html;
error_page 404 /404.jpg;
}
}
[root@web01 code]# ll 404.jpg
-rw-r--r-- 1 root root 9564 Mar 10 16:07 404.jpg
3.配置负载证明错误页面是由web端返回的
[root@lb01 conf.d]# vim err.conf
upstream errweb {
server 172.16.1.7:80;
}
server {
listen 80;
server_name error.linux.com;
location / {
proxy_pass http://errweb;
include proxy_params;
access_log /tmp/nginx.log;
}
}
4.访问php错误页面跳转
server {
listen 80;
server_name blog.linux.com;
location / {
root /code/wordpress;
index index.php;
error_page 404 403 /404.jpg;
}
location ~* \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
if (!-e $request_filename) {
rewrite (.*) /code/wordpress/404.jpg;
}
}
}
本文来自博客园,作者:ଲ一笑奈&何,转载请注明原文链接:https://www.cnblogs.com/zeny/p/15121606.html