nginx-01
代理
正向代理
客户端将请求转发给正代服务器,正向代理服务器再负责转发给服务端,响应时服务端先响应给正向代理服务器,正向代理服务器再转发给对应的客户端。也就是说,正向代理可以但不限于为局域网内客户端做代理,它扮演的角色类似于NAT。
反向代理
为服务端转发请求,客户端将请求发送至反向代理服务器,反向代理服务器再将请求转发给真正的服务器以处理请求,响应时后端真正的服务器将处理结果发送给反向代理,再由反向代理构建响应并响应给客户端。
透明代理
透明代理其实也叫做内网代理(inline proxy)、拦截代理(intercepting proxy)以及强制代理(force proxy)。透明代理和正向代理的行为很相似,但细节上有所不同。透明代理将拦截客户端发送的请求,拦截后自己代为访问服务端,获取响应结果后再由透明代理交给客户端。一般公司内的上网行为管理软件就是透明代理。
nginx介绍
nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
nginx模块
结构上
核心模块
HTTP模块、EVENT模块和MAIL模块
基本模块
HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块
第三方模块
HTTP Upstream模块、Request Hash模块、Notice模块和HTTP Access Key模块
功能上
Handlers(处理器模块)
此类模块直接处理请求,并进行输出内容和修改headers信息等操作。handlers处理器模块一般只能有一个
Filters(过滤器模块)
此类模块主要对其他处理器模块输出的内容进行修改操作,最后由nginx输出
Proxies(代理器模块)
就是nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务比如fastcgi等操作交互,实现服务代理和负载均衡等功能
nginx原理
nginx的模块直接被编译进nginx,因此属于静态编译方式。
启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。
nginx安装部署
nginx安装
创建系统用户
[root@vm3 ~]# useradd -r -M -s /sbin/nologin nginx
[root@vm3 ~]# id nginx
uid=995(nginx) gid=992(nginx) groups=992(nginx)
安装依赖
[root@vm3 ~]# yum -y install tar make wget pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
创建日志目录
[root@vm3 ~]# mkdir -p /var/log/nginx
[root@vm3 ~]# chown -R nginx.nginx /var/log/nginx
[root@vm3 ~]# ll /var/log/nginx -d
drwxr-xr-x. 2 nginx nginx 6 Dec 7 00:31 /var/log/nginx
下载nginx包
[root@vm3 ~]# cd /opt/
[root@vm3 opt]# ls
[root@vm3 opt]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
.........................................................
nginx-1.18.0.tar.gz 100%[=====================================>] 1015K 18.1KB/s in 49s
2020-12-07 00:34:54 (20.9 KB/s) - ‘nginx-1.18.0.tar.gz’ saved [1039530/1039530]
[root@vm3 opt]# ls
nginx-1.18.0.tar.gz
编译安装
[root@vm3 opt]# tar xf nginx-1.18.0.tar.gz
[root@vm3 opt]# ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@vm3 opt]# cd nginx-1.18.0
[root@vm3 nginx-1.18.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@vm3 nginx-1.18.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@vm3 nginx-1.18.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l)
[root@vm3 nginx-1.18.0]# make install
nginx配置
nginx服务控制
//服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
配置环境变量
[root@vm3 nginx-1.18.0]# cd /usr/local/
[root@vm3 local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@vm3 local]# cd nginx
[root@vm3 nginx]# ls
conf html logs sbin
[root@vm3 nginx]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@vm3 nginx]# source /etc/profile.d/nginx.sh
[root@vm3 nginx]# nginx
[root@vm3 nginx]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
nginx动态加载模块
流程
1.下载并解压需要添加的模块
2.查看当前nginx的编译参数(nginx -V)
3.进入nginx解压目录重新编译(编译参数为原参数 + --add-module=添加模的解压目录,不要执行make install)
4.备份原nginx二进制文件(...nginx/sbin/nginx),以便回滚
5.拷贝nginx解压目录里重新编译的objs/nginx文件至...nginx/sbin/nginx
6.重新启动nginx服务
添加echo-nignx-module-master模块
- 下载并解压模块
echo-nignx-module-master地址
[root@vm3 opt]# wget https://github.com/openresty/echo-nginx-module/archive/master.zip
[root@vm3 opt]# unzip echo-nginx-module-master.zip
[root@vm3 opt]# ls
echo-nginx-module-master nginx-1.18.0 zabbix-5.2.0.tar.gz
echo-nginx-module-master.zip nginx-1.18.0.tar.gz
- 查看当前nginx编译参数
[root@vm3 opt]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.1c FIPS 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
- 重新编译
[root@vm3 opt]# cd nginx-1.18.0
[root@vm3 nginx-1.18.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@vm3 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/opt/echo-nginx-module-master
....................................
[root@vm3 nginx-1.18.0]# make
- 备份原nginx文件
[root@vm3 nginx-1.18.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.backup
[root@vm3 nginx-1.18.0]# ls /usr/local/nginx/sbin/
nginx nginx.backup
- 拷贝新的nginx文件
[root@vm3 nginx-1.18.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@vm3 nginx-1.18.0]# cd objs
[root@vm3 objs]# ls
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[root@vm3 objs]# cp nginx /usr/local/nginx/sbin/nginx
- 重新启动nginx
[root@vm3 objs]# nginx -s stop
[root@vm3 objs]# nginx
[root@vm3 objs]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.1c FIPS 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/opt/echo-nginx-module-master
- 测试
[root@vm3 conf]# vim nginx.conf
...............................
location /test {
echo "this is a test!";
}
...............................
[root@vm3 conf]# nginx -s reload
[root@vm3 conf]# curl http://192.168.225.130/test
this is a test!
nginx配置文件
配置文件 | 作用 |
---|---|
nginx.conf | nginx主配置文件 |
mime.types | 配置MIME类型 |
fastcgi.conf | 与fastcgi相关的配置 |
proxy.conf | 与proxy相关的配置 |
sites.conf | 配置nginx提供的网站,虚拟主机 |
- 默认启动nginx时,使用的配置文件是:conf目录下nginx.conf文件
- 可以在启动nginx时通过-c选项来指定要读取的配置文件
nginx.conf
nginx.conf配置文件内容如下:
- main配置段:全局配置,其中main配置段中可能包含event配置段
- event{}:定义event模型工作特性
- http{}:定义http协议相关配置
main配置段
用于调试,定位问题
- daemon {on|off}; //是否以守护进程方式运行nginx,调试时应设置为off
[root@vm3 conf]# pwd
/usr/local/nginx/conf
[root@vm3 conf]# vim nginx.conf
...............
daemon off;
...............
[root@vm3 conf]# nginx ##启动时该终端会卡住
[root@vm3 conf]# vim nginx.conf
...............
daemon on;
...............
[root@vm3 conf]# nginx
[root@vm3 conf]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@vm3 conf]# ps -ef|grep nginx
root 28422 1 0 15:46 ? 00:00:00 nginx: master process nginx
nginx 28423 28422 0 15:46 ? 00:00:00 nginx: worker process
root 28426 1234 0 15:47 pts/0 00:00:00 grep --color=auto nginx
- master_process {on|off}; //是否以master/worker模型来运行nginx
[root@vm3 conf]# vim nginx.conf
..........................
daemon on;
master_process off;
..........................
[root@vm3 conf]# nginx -s stop
[root@vm3 conf]# nginx
[root@vm3 conf]# ps -ef|grep nginx
root 28434 1 0 15:54 ? 00:00:00 nginx
root 28436 1234 0 15:54 pts/0 00:00:00 grep --color=auto nginx
[root@vm3 conf]# vim nginx.conf
..........................
daemon on;
master_process on;
..........................
[root@vm3 conf]# nginx -s stop
[root@vm3 conf]# nginx
[root@vm3 conf]# ps -ef|grep nginx
root 28448 1 0 15:57 ? 00:00:00 nginx: master process nginx
nginx 28449 28448 0 15:57 ? 00:00:00 nginx: worker process
root 28451 1234 0 15:57 pts/0 00:00:00 grep --color=auto nginx
- error_log 位置 级别; //配置错误日志
位置 | 级别 |
---|---|
file(文件) stderr(标准错误) syslog:server=addres[,parameter=value] memory:size |
debug:若要使用debug级别,需要在编译nginx时使用--with-debug选项 info notice warn error crit alert emerg |
[root@vm3 conf]# vim nginx.conf
...................................
worker_processes 1;
daemon on;
master_process on;
error_log logs/error.log;
................................
[root@vm3 conf]# nginx -s stop
[root@vm3 conf]# nginx
[root@vm3 html]# mv index.html index.htmlsf
[root@vm3 html]# ll
total 8
-rw-r--r--. 1 root root 494 Dec 7 00:47 50x.html
-rw-r--r--. 1 root root 612 Dec 7 00:47 index.htmlsf
[root@vm3 conf]# tail -f ../logs/error.log
2020/12/17 16:16:00 [error] 28541#0: *3 directory index of "/usr/local/nginx/html/" is forbidden, client: 192.168.225.1, server: localhost, request: "GET / HTTP/1.1", host: "192.168.225.130"
正常运行必备配置参数
- user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组
[root@vm3 conf]# vim nginx.conf
..............................
user nobody;
worker_processes 1;
daemon on;
master_process on;
error_log logs/error.log;
..............................
[root@vm3 conf]# nginx -s stop
[root@vm3 conf]# nginx
[root@vm3 conf]# ps -ef| grep nginx
root 28724 1 0 16:25 ? 00:00:00 nginx: master process nginx
nobody 28725 28724 0 16:25 ? 00:00:00 nginx: worker process
root 28727 1234 0 16:25 pts/0 00:00:00 grep --color=auto nginx
[root@vm3 conf]# vim nginx.conf
..............................
user nginx;
worker_processes 1;
daemon on;
master_process on;
error_log logs/error.log;
..............................
[root@vm3 conf]# nginx -s stop
[root@vm3 conf]# nginx
[root@vm3 conf]# ps -ef| grep nginx
root 28716 1 0 16:24 ? 00:00:00 nginx: master process nginx
nginx 28717 28716 0 16:24 ? 00:00:00 nginx: worker process
root 28719 1234 0 16:24 pts/0 00:00:00 grep --color=auto nginx
- pid /path/to/pid_file; //指定nginx守护进程的pid文件
[root@vm3 conf]# vim nginx.conf
................................
user nginx;
worker_processes 1;
daemon on;
master_process on;
error_log logs/error.log;
pid logs/nginx.pid;
.................................
- worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024
[root@vm3 conf]# vim nginx.conf
............................
user nginx;
worker_processes 1;
daemon on;
master_process on;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 500;
............................
- worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
优化性能配置参数
- worker_processes n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
[root@vm3 conf]# vim nginx.conf
..................................
user nginx;
daemon on;
master_process on;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 500;
worker_processes 3;
................................
[root@vm3 conf]# nginx
[root@vm3 conf]# ps -ef|grep nginx
root 28797 1 0 16:50 ? 00:00:00 nginx: master process nginx
nginx 28798 28797 0 16:50 ? 00:00:00 nginx: worker process
nginx 28799 28797 0 16:50 ? 00:00:00 nginx: worker process
nginx 28800 28797 0 16:50 ? 00:00:00 nginx: worker process
root 28802 1234 0 16:50 pts/0 00:00:00 grep --color=auto nginx
- worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:
0000 0001 //第一颗cpu核心
0000 0010 //第二颗cpu核心
0000 0100 //第三颗cpu核心
- timer_resolution interval; //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
- worker_priority number; //指明worker进程的nice值
[root@vm3 conf]# vim nginx.conf
.......................
user nginx;
daemon on;
master_process on;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 500;
worker_processes 3;
worker_priority 10;
..............................
[root@vm3 conf]# nginx -s stop
[root@vm3 conf]# nginx
[root@vm3 conf]# ps -elf| grep nginx
1 S root 28815 1 0 80 0 - 20050 - 16:58 ? 00:00:00 nginx: master process nginx
5 S nginx 28816 28815 0 90 10 - 27931 do_epo 16:58 ? 00:00:00 nginx: worker process
5 S nginx 28817 28815 0 90 10 - 27931 do_epo 16:58 ? 00:00:00 nginx: worker process
5 S nginx 28818 28815 0 90 10 - 27931 do_epo 16:58 ? 00:00:00 nginx: worker process
0 S root 28820 1234 0 80 0 - 55475 - 16:58 pts/0 00:00:00 grep --color=auto nginx
event{}配置段
accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
lock_file file; //accept_mutex用到的互斥锁锁文件路径
use [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择
worker_connections #; //每个进程能够接受的最大连接数,默认1024
网络连接相关的配置
keepalive_timeout number; //长连接的超时时长,默认为65s
keepalive_requests number; //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none]; //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off; //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number; //读取http请求报文首部的超时时长
client_body_timeout number; //读取http请求报文body部分的超时时长
send_timeout number; //发送响应报文的超时时长
http{}配置段
http {//协议级别
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
upstream {//负载均衡配置
...
}
server {//服务器级别,每个server类似于httpd中的一个<VirtualHost>
.........;
location / {//请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系
.........;
}
}
}
协议级别
- log_format,访问日志格式
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;
负载均衡配置
在upstream段内,定义一个服务器列表,默认的方式是轮询.
ip_hash:确定同一个访问者发出的请求总是由同一个后端服务器来处理.
upstream idfsoft.com {
ip_hash;
server 127.0.0.1:9080 weight=5;
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:1111;
}
- 定义好upstream后,需要在server段内添加如下内容:
server {
location / {
proxy_pass http://idfsoft.com;
}
}
服务器级别
- 多个server匹配顺序
1.先做精确匹配检查
2.左侧通配符匹配检查,如*.idfsoft.com
3.右侧通配符匹配检查,如mail.*
4.正则表达式匹配检查,如~ ^.*\.idfsoft\.com$
5.default_server
- 配置格式
server {
listen address[:port]; ##listen port;
server_name NAME [...];
[root path];
error_page code [...] [=code] URI/@name ##根据http响应状态码来指明特用的错误页面;[=code]以指定的响应码进行响应,而不是默认的原来的响应。
https server
server {
listen 443 ssl;
server_name www.idfsoft.com;
ssl_certificate /usr/local/nginx/cert/nginx.crt; ##证书路径
ssl_certificate_key /usr/local/nginx/cert/nginx.key; ##私钥路径
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
location级别
允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
location [修饰符] URI/别名 {
root path; ##设置资源路径映射,用于指明请求的URL所对应的资源所在的文件系统上的起始路径
alias path; ##用于location配置段,定义路径别名
index file; ##默认主页
}
}
- 常用修饰符
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始 不同的是,如果模式匹配,那么就停止搜索其他模式 ,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以 由内部产生的请求来访问,如try_files或error_page等 |
- 匹配流程
location有两种表示形似,一种是使用前缀字符(^,无修饰符),另一种是使用正则(,~*)
1.首先检查使用前缀字符定义的location,记录最长匹配的项
2.若找到精确匹配(=)的location,则结束查找,使用其配置。
3.否则就按顺序查找使用正则定义的location,匹配则结束,并使用其配置
4.若没有匹配到正则location,则使用前面记录的最长匹配前缀字符location
fastcgi相关参数
- lnmp:php配置
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
访问控制
allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny:设定禁止哪台或哪些主机访问,多个参数间用空格隔开
访问控制可以定义在location级别,server级别,协议级别(http段内server段外),定义在哪则在娜生效。
- 白名单
allow 192.168.1.1/32 172.16.0.0/16;
deny all;
- 黑名单
deny 192.168.1.1/32 172.16.0.0/16
allow all;
用户认证
auth_basic "欢迎信息";
auth_basic_user_file "/path/to/user_auth_file"
同访问控制一样可以定义在多种级别中,但是仅认证一次
user_auth_file(文件名可以自定义)内容为
- username:password(密码为加密的密码串,建议用htpasswd创建该文件)
[root@vm3 nginx]# htpasswd -c -m /usr/local/nginx/nginx_passwd admin
New password:
Re-type new password:
Adding password for user admin
[root@vm3 nginx]# pwd
/usr/local/nginx
[root@vm3 nginx]# ls
client_body_temp fastcgi_temp logs proxy_temp scgi_temp
conf html nginx_passwd sbin uwsgi_temp
[root@vm3 nginx]# cat nginx_passwd
admin:$apr1$XrTNS0Je$CORlattsZ3j0mNNUdFWSE0
[root@vm3 conf]# vim nginx.conf
.........................
server {
listen 80;
server_name localhost;
auth_basic "hello!";
auth_basic_user_file "/usr/local/nginx/nginx_passwd";
.................................
nginx状态界面
- 开启status:
.....................................
location /status {
stub_status {on | off};
allow 192.168.225.130/32; ##一般仅允许本机访问,以免泄露服务器信
deny all;
}
....................................
- 状态页面详情
状态码 | 表示的意义 |
---|---|
Active connections | 当前所有处于打开状态的连接数 |
accepts | 总共处理了多少个连接 |
handled | 成功创建多少握手 |
requests | 总共处理了多少个请求 |
Reading | nginx读取到客户端的Header信息数 表示正处于接收请求状态的连接数 |
Writing | nginx返回给客户端的Header信息数 表示请求已经接收完成,且正处于处理请求或发送响应的过程中的连接数 |
Waiting | 开启keep-alive的情况下,这个值等于 active - (reading + writing), 意思就是Nginx已处理完正在等候下一次请求指令的驻留连接 |
- 测试
##192.168.225.130上测试
[root@vm3 nginx]# curl -u admin:123456 http://192.168.225.130/status
Active connections: 1
server accepts handled requests
44 44 46
Reading: 0 Writing: 1 Waiting: 0
##其他机器上测试
[wisan@fyj ~]$ curl -u admin:123456 http://192.168.225.130/status
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
if
- 语法:if (condition) {...} ##应用于server、location段
- condition:
1.变量名(值为空串或以0开始则为false,否则为true)
2.比较表达式(以变量为操作数,= |!=)
3.正则表达式的模式匹配(~:区分大小写;~*:不区分大小写;!~|!~*为取反)
4.测试是否为文件(-f,!-f)
5.测试是否为目录(-d,!-d)
6.测试文件是否存在(-e,!-e)
7.检查文件是否有执行权限(-x,!-x)
rewirte
rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url,也有利于搜索引擎优化(SEO)。nginx使用的语法源于Perl兼容正则表达式(PCRE)库。
rewirte语法
- rewrite regex replacement [flag];
##regex:正则表达式regex来匹配请求的urI,如果匹配成功,则使用replacement。
##replacement:可以是某个路径,也可以是某个URL。
##flag
1.last:表示当前的匹配结束,可以由UserAgent重新对重写后的URL再一次发起请求继续重定向进行下一个匹配。
2.break:表示当前匹配结束,且不再进行下一轮匹配
3.redirect:以临时重定向的HTTP状态302返回新的URL
4.permanent:以永久重定向的HTTP状态301返回新的URL
rewirte配置
location /imgs {
rewrite ^/imgs/(.*) /images/$1 break;
}
location /test1 {
rewrite /test1/(.*) http://www.$1.com;
}
rewrite实现分离
- 对于不同浏览器实现流量分离
if ($http_user_agent ~ Firefox) {
rewrite ^(.*)$ /firefox/$1 break;
}
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_user_agent ~ Chrome) {
rewrite ^(.*)$ /chrome/$1 break;
}
rewirte实现防盗链
- 语法: valid_referers none | blocked | server_names | string ...;
valid_referers指定合法的来源'referer',他决定了内置变量$invalid_referer的值,如果referer头部包含在指定的列表中,这个变量被设置为0,否则设置为1.
1.none:“Referer” 来源头部为空的情况(-)
2.blocked:Referer”来源头部不为空,但是里面的值被代理或者防火墙删除了,这些值都不以http://或者https://开头.
3.server_names:指定Referer来源头部包含的域名
4.string:定义服务器名或者可选的URI前缀
location ~* \.(jpg|gif|jpeg|png)$ {
valid_referers none blocked www.wisan.com;
if ($invalid_referer) { ##如果访问请求来源不是由valid_referers指定的则invalid_referer值为true,否则为false.
rewrite ^/ http://www.wisan.com/403.html;
}
}
反向代理
nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。
nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。
但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储