nginx笔记
Yum安装nginx
得先去配置nginx的Yum源,去到官网找,然后直接yum -y install 下载
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key
编译安装
yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel 编译参数 # 查看 nginx 安装的模块 [root@localhost ~]#/usr/local/nginx/sbin/nginx -V # 模块参数具体功能 --with-cc-opt='-g -O2 -fPIE -fstack-protector //设置额外的参数将被添加到CFLAGS变量。(FreeBSD或者ubuntu使用) --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/local/nginx //指向安装目录 --conf-path=/etc/nginx/nginx.conf //指定配置文件 --http-log-path=/var/log/nginx/access.log //指定访问日志 --error-log-path=/var/log/nginx/error.log //指定错误日志 --lock-path=/var/lock/nginx.lock //指定lock文件 --pid-path=/run/nginx.pid //指定pid文件 --http-client-body-temp-path=/var/lib/nginx/body //设定http客户端请求临时文件路径 --http-fastcgi-temp-path=/var/lib/nginx/fastcgi //设定http fastcgi临时文件路径 --http-proxy-temp-path=/var/lib/nginx/proxy //设定http代理临时文件路径 --http-scgi-temp-path=/var/lib/nginx/scgi //设定http scgi临时文件路径 --http-uwsgi-temp-path=/var/lib/nginx/uwsgi //设定http uwsgi临时文件路径 --with-debug //启用debug日志 --with-pcre-jit //编译PCRE包含“just-in-time compilation” --with-ipv6 //启用ipv6支持 --with-http_ssl_module //启用ssl支持 --with-http_stub_status_module //获取nginx自上次启动以来的状态 --with-http_realip_module //允许从请求标头更改客户端的IP地址值,默认为关 --with-http_auth_request_module //实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。 --with-http_addition_module //作为一个输出过滤器,支持不完全缓冲,分部分响应请求 --with-http_dav_module //增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启 --with-http_geoip_module //使用预编译的MaxMind数据库解析客户端IP地址,得到变量值 --with-http_gunzip_module //它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。 --with-http_gzip_static_module //在线实时压缩输出数据流 --with-http_image_filter_module //传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到) --with-http_spdy_module //SPDY可以缩短网页的加载时间 --with-http_sub_module //允许用一些其他文本替换nginx响应中的一些文本 --with-http_xslt_module //过滤转换XML请求 --with-mail //启用POP3/IMAP4/SMTP代理模块支持 --with-mail_ssl_module //启用ngx_mail_ssl_module支持启用外部模块支持
配置文件/etc/nginx/nginx.conf
# 全局参数设置 worker_processes 4; #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同 error_log logs/error.log; #指定错误日志 worker_rlimit_nofile 102400; #设置一个nginx进程能打开的最大文件数 pid /var/run/nginx.pid; events { worker_connections 1024; #设置一个进程的最大并发连接数 } # 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 /var/log/nginx/access.log main; #设置访问日志的位置和格式 sendfile on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载 gzip on; #是否开启gzip压缩,将注释去掉开启 keepalive_timeout 65; #设置长连接的超时时间 # 虚拟服务器的相关设置 server { listen 80; #设置监听的端口 server_name localhost; #设置绑定的主机名、域名或ip地址 charset koi8-r; # 设置编码字符 location / { root /var/www/nginx; #设置服务器默认网站的根目录位置,需要手动创建 index index.html index.htm; #设置默认打开的文档 } error_page 500 502 503 504 /50x.html; #设置错误信息返回页面 location = /50x.html { root html; #这里的绝对位置是/usr/local/nginx/html } } } nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块。在http块中又包含http全局块、多个server块。每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。
通过 nginx 命令控制 nginx 服务
nginx -c /path/nginx.conf # 以特定目录下的配置文件启动nginx: nginx -s reload # 修改配置后重新加载生效 nginx -s reopen # 重新打开日志文件 nginx -s stop # 快速停止nginx nginx -s quit # 完整有序的停止nginx nginx -t # 测试当前配置文件是否正确 nginx -t -c /path/to/nginx.conf # 测试特定的nginx配置文件是否正确
实现nginx开机自启,yum安装systemctl启动方式
[root@localhost ~]# vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac 添加权限 chmod +x /etc/init.d/nginx 重新加载系统启动文件 systemctl daemon-reload systemctl start nginx [root@localhost ~]# /sbin/chkconfig nginx on ---开机启动
日志文件解析
$remote_addr, $http_x_forwarded_for 记录客户端IP地址 $remote_user记录客户端用户名称 $request记录请求的URL和HTTP协议(GET,POST,DEL,等) $status记录请求状态 $body_bytes_sent发送给客户端的字节数,不包括响应头的大小;该变量与Apache模块mod_log_config里的“%B”参数兼容。 $bytes_sent发送给客户端的总字节数。 $connection连接的序列号。 $connection_requests 当前通过一个连接获得的请求数量。 $msec 日志写入时间。单位为秒,精度是毫秒。 $pipe如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。 $http_referer 记录从哪个页面链接访问过来的 $http_user_agent记录客户端浏览器相关信息 $request_length请求的长度(包括请求行,请求头和请求正文)。 $request_time 请求处理时间,单位为秒,精度毫秒;从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。 $time_iso8601 ISO8601标准格式下的本地时间。 $time_local通用日志格式下的本地时间。 log_format main 'remote_addr - remote_user [time_local] "request" ' 'status body_bytes_sent "$http_referer" ' '"http_user_agent" "http_x_forwarded_for"'; 客户端IP,客户端名称 时间 请求的URL和http协议,响应状态码 发送客户端的主体内容大小,客户端跳转地址,客户端浏览器信息,客户端真实IP
nginx 虚拟主机配置
基于ip,端口,域名 的虚拟主机
server { listen 192.168.235.130:80; server_name web.mingge.com; location / { root /var/www/nginx/; index index.html index.htm; limit_rate 2k; } server { listen 192.168.235.131:81; server_name web.testpm.com; location / { root /1000phone/html/; index index.html index.htm; } } } 可以浏览器输入:web.testpm.com:81
负载均衡和动静分离
负载均衡
代理端配置: upstream testweb { ip_hash; server 10.0.105.199:80 weight=2 max_fails=2 fail_timeout=2s; server 10.0.105.202:80 weight=2 max_fails=2 fail_timeout=2s; } server { listen 80; server_name www.test.com; charset utf-8; #access_log logs/host.access.log main; location / { proxy_pass http://testweb; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server端正常配置即可: server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } server { listen 8081; server_name localhost; location / { root /var/www/nginx/html; index index.html index.htm; }
proxy_pass :真实服务器的地址,可以是ip也可以是域名和url地址 proxy_redirect :如果真实服务器使用的是的真实IP:非默认端口。则改成IP:默认端口。 proxy_set_header:重新定义或者添加发往后端服务器的请求头 proxy_set_header X-Real-IP :启用客户端真实地址(否则日志中显示的是代理在访问网站) proxy_set_header X-Forwarded-For:记录代理地址 proxy_connect_timeout::后端服务器连接的超时时间发起三次握手等候响应超时时间 proxy_send_timeout:后端服务器数据回传时间就是在规定时间之内后端服务器必须传完所有的数据 proxy_read_timeout :nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接
nginx负载均衡配置状态参数 * down,表示当前的server暂时不参与负载均衡。 * backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,如果故障机恢复,backup还充当备机,因此这台机器的压力最轻。 * max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。 * fail_timeout,在经历了max_fails次失败后,暂停服务的时间**单位秒**。max_fails可以和fail_timeout一起使用。 upstream myweb { server 172.17.14.2:8080 weight=2 max_fails=1 fail_timeout=10; server 172.17.14.3:8080 weight=1 max_fails=1 fail_timeout=10; } 把其中一台nginx停止,之后,页面只能访问到另外一台,把停掉的nginx拉起来,也不会正常提供服务,得等待服务暂停时间过后才能正常提供服务
动静分离
代理服务器: 1.配置nginx反向代理upstream; upstream static { server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s; } upstream php { server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s; } server { listen 80; server_name localhost; #动态资源加载 location ~ \.(php|jsp)$ { proxy_pass http://php; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #静态资源加载 location ~ \.(html|gif|jpg|png|bmp|swf|css|js)$ { proxy_pass http://static; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 静态资源配置 server { listen 80; server_name localhost; location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg) { root /home/www/nginx; index index.html; } } 动态资源配置: yum 安装php7.1 [root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm [root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm [root@nginx-server ~]#yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y [root@nginx-server ~]#yum install -y php71w-fpm [root@nginx-server ~]#systemctl start php-fpm [root@nginx-server ~]#systemctl enable php-fpm 编辑nginx的配置文件: server { listen 80; server_name localhost; location ~ \.php$ { root /home/nginx/html; #指定网站目录 fastcgi_pass 127.0.0.1:9000; #指定访问地址 fastcgi_index index.php; #指定默认文件 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #站点根目录,取决于root配置项 include fastcgi_params; #包含nginx常量定义 } } 浏览器访问的跟动态静态文件.html .php
server端获取真实地址配置
修改nginx日志格式,添加$http_x_forwarded_for 代理端: proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; #proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; server端: set_real_ip_from 192.168.200.10; #代理的地址 real_ip_header X-Forwarded-For; real_ip_recursive on;
四层tcp负载均衡
stream { upstream myweb { hash $remote_addr consistent; server 172.17.14.2:8080; server 172.17.14.3:8080; } server { listen 82; proxy_connect_timeout 10s; proxy_timeout 30s; proxy_pass myweb; } } ps:配置到代理端主配置文件,stream跟http同级需要把http块注释掉,注释玩也可以放到子配置文件
会话保持
1、ip_hash upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com down; } 2、sticky_cookie_insert upstream backend { server backend1.example.com; server backend2.example.com; sticky expires=1h domain=3evip.cn path=/; } 与ip_hash不同之处在于,它不是基于IP来判断客户端的,而是**基于cookie来判断**。 3、jvm_route方式
nginx防盗链
防止未经允许的网站盗链图片、文件 # valid_referers 使用方式 Syntax: valid_referers none | blocked | server_names | string ...; Default: — Context: server, location none : 允许没有http_referer的请求访问资源; blocked : 允许不是http://开头的,不带协议的请求访问资源 server_names : 只允许指定ip/域名来的请求访问资源(白名单);
配置nginx配置文件,并上传图片 [root@nginx-server html]# vim /etc/nginx/conf.d/nginx.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; #valid_referer后面跟的域名或者ip都是允许登录的 valid_referers none blocked *.qf.com 10.0.105.202; if ($invalid_referer) { return 404; } } } } 重载nginx服务 第二台机器客户端 配置nginx访问页面 创建页面 vim nginx.conf server { listen 80; server_name localhost; location / { root /var/www/html; index index.html; } } [root@nginx-server nginx]# vim index.html <html> <head> <meta charset="utf-8"> <title>qf.com</title> </head> <body style="background-color:red;"> <img src="http://10.0.105.202/test.jpg"/> </body> </html> 测试不带http_referer: [root@nginx-server nginx]# curl -I "http://10.0.105.202/test1.png" HTTP/1.1 200 OK Server: nginx/1.16.0 Date: Thu, 27 Jun 2019 16:21:13 GMT Content-Type: image/png Content-Length: 235283 Last-Modified: Thu, 27 Jun 2019 11:27:11 GMT Connection: keep-alive ETag: "5d14a80f-39713" Accept-Ranges: bytes 测试带非法http_refer: -e 请求的来源(上一个跳点) [root@nginx-server nginx]# curl -e http://www.baidu.com -I "http://10.0.105.202/test.jpg" HTTP/1.1 403 Forbidden Server: nginx/1.16.0 Date: Thu, 27 Jun 2019 16:22:32 GMT Content-Type: text/html Content-Length: 153 Connection: keep-alive 测试带合法的http_refer: [root@nginx-server nginx]# curl -e http://10.0.105.202 -I "http://10.0.105.202/test.jpg" HTTP/1.1 200 OK Server: nginx/1.16.0 Date: Thu, 27 Jun 2019 16:23:21 GMT Content-Type: image/jpeg Content-Length: 27961 Last-Modified: Thu, 27 Jun 2019 12:28:51 GMT Connection: keep-alive ETag: "5d14b683-6d39" Accept-Ranges: bytes
nginx 地址重写 rewrite
if 判断语法
应用环境 server,location if (condition) { … } if 可以支持如下条件判断匹配符号 ~ 正则匹配 (区分大小写) ~* 正则匹配 (不区分大小写) !~ 正则不匹配 (区分大小写) !~* 正则不匹配 (不区分大小写) -f 和!-f 用来判断是否存在文件 -d 和!-d 用来判断是否存在目录 -e 和!-e 用来判断是否存在文件或目录 -x 和!-x 用来判断文件是否可执行 在匹配过程中可以引用一些Nginx的全局变量 $args 请求中的参数; $document_root 针对当前请求的根路径设置值; $host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名; $limit_rate 对连接速率的限制; $request_method 请求的方法,比如"GET"、"POST"等; $remote_addr 客户端地址; $remote_port 客户端端口号; $remote_user 客户端用户名,认证用; $request_filename 当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images/a.jpg) $request_uri 当前请求的文件路径名(不带网站的主目录/images/a.jpg) $query_string 与$args相同; $scheme 用的协议,比如http或者是https $server_protocol 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; $server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); $server_name 请求到达的服务器名; $document_uri 与$uri一样,URI地址; $server_port 请求到达的服务器端口号;
Rewrite flag
**rewrite** 指令根据表达式来重定向URI,或者修改字符串。可以应用于**server,location, if**环境下每行rewrite指令最后跟一个flag标记,支持的flag标记有 last 相当于Apache里的[L]标记,表示完成rewrite。默认为last(从server往下再次匹配)。 break 本条规则匹配完成后,终止匹配,不再匹配后面的规则 redirect 返回302临时重定向,浏览器会显示跳转后的内容 permanent 返回301永久重定向,浏览器地址会显示跳转后URL地址
Rewrite匹配参考示例
本地解析host文件 # http://www.testpm.com/a/1.html ==> http://www.testpm.com/b/2.html location /a { root /html; index 1.html index.htm; rewrite .* /b/2.html permanent; } location /b { root /html; index 2.html index.htm; } 例: # http://www.testpm.com/2019/a/1.html ==> http://www.testpm.com/2018/a/1.html location /2019/a { root /var/www/html; index 1.html index.hml; rewrite ^/2019/(.*)$ /2018/$1 permanent; } location /2018/a { root /var/www/html; index 1.html index.htl; } 例: # http://www.qf.com/a/1.html ==> http://jd.com location /a { root /html; if ($host ~* www.qf.com ) { rewrite .* http://jd.com permanent; } } 例: # http://www.qf.com/a/1.html ==> http://jd.com/a/1.html location /a { root /html; if ( $host ~* qf.com ){ rewrite .* http://jd.com$request_uri permanent; } }
set 指令
set 指令是用于定义一个变量,并且赋值 应用环境server,location,if 例: #http://alice.testpm.com ==> http://www.testpm.com/alice #http://jack.testpm.com ==> http://www.testpm.com/jack [root@nginx-server conf.d]# cd /usr/share/nginx/html/ [root@nginx-server html]# mkdir jack alice [root@nginx-server html]# echo "jack.." >> jack/index.html [root@nginx-server html]# echo "alice.." >> alice/index.html windows上做解析: 本地解析域名host文件 10.0.105.202 www.testpm.com 10.0.105.202 alice.testpm.com 10.0.105.202 jack.testpm.com 编辑配置文件: server { listen 80; server_name www.testpm.com; location / { root /usr/share/nginx/html; index index.html index.htm; if ( $host ~* ^www.testpm.com$) { break; } #$host定义ip 匹配以所有的的为开头以.com结尾 if ( $host ~* "^(.*)\.testpm\.com$" ) { set $user $1; rewrite .* http://www.testpm.com/$user permanent; } } location /jack { root /usr/share/nginx/html; index index.html index.hml; } location /alice { root /usr/share/nginx/html; index index.html index.hml; } }
return 指令
return 指令用于返回状态码给客户端 server,location,if 例:如果访问的.sh结尾的文件则返回403操作拒绝错误 server { listen 80; server_name www.testpm.cn; #access_log /var/log/nginx/http_access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location ~* \.sh$ { return 403; } }
last,break详解
[root@localhost test]# cat /etc/nginx/conf.d/last_break.conf server { listen 80; server_name localhost; location ~ /last { root /usr/share/nginx/html; index index.html index.htm; rewrite ^(.*)$ /result last; } location ~ /break { root /usr/share/nginx/html; index index.html; rewrite ^(.*)$ /result/index.html break; } location / { root /usr/share/nginx/html; index index.html; } location ~ /result { root /usr/share/nginx/html; index index.html; rewrite ^(.*)$ /test; } location ~ /test { root /usr/share/nginx/html; index index.html; } } [root@localhost conf.d]# cd /usr/share/nginx/html/ [root@localhost html]# mkdir test result break last [root@localhost html]# echo "last" > last/index.html [root@localhost html]# echo "break" > break/index.html [root@localhost html]# echo "test" > test/index.html [root@localhost html]# echo "result" > result/index.html http://10.0.105.196/break/break.html http://10.0.105.196/last/last.html last 标记在本条 rewrite 规则执行完后,会对其所在的 server { … } 标签重新发起请求; break 标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配; 使用 alias 指令时,必须使用 last 使用 proxy_pass 指令时,则必须使用break。
nginx的localtion指令详解
location 区段
location 是在 server 块中配置,根据不同的 URI(统一资源标识符)URL(同统一资源定位符) 使用不同的配置,来处理不同的请求。location 是有顺序的,会被第一个匹配的location 处理。 = 表示精确匹配,优先级也是最高的 ^~ 表示uri以某个常规字符串开头,理解为匹配url路径即可 ~ 表示区分大小写的正则匹配 ~* 表示不区分大小写的正则匹配 !~ 表示区分大小写不匹配的正则 !~* 表示不区分大小写不匹配的正则 / 通用匹配,任何请求都会匹配到 = 大于 ^~ 大于 ~|~*|!~|!~* 大于 / 多个location配置的情况下匹配顺序为:首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。 ================================================ (1) =:表示完全匹配; (2) ^~:匹配URI的前缀,并且后面的正则表达式不再匹配,如果一个URI同时满足两个规则的话,匹配最长的规则; (3) ~:匹配正则表达式,大小写敏感; (4) ~*:匹配正则表达式,大小写不敏感; 优先级:(1)> (2) > (3) = (4) 1、没有修饰符 表示:必须以指定模式开始 按顺序 2、=表示:必须与指定的模式精确匹配 server { listen 80; server_name www.testpm.cn; access_log /var/log/nginx/http_access.log main; location / { root /usr/share/nginx/html; index a.html index.htm; } location = / { root /usr/share/nginx/html; index b.html index.htm; } } 3、~ 表示:指定的正则表达式要区分大小写 4、~* 表示:指定的正则表达式不区分大小写
root 、alias 指令区别
alias 是一个目录别名的定义, root 则是最上层目录的定义。 还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无 location、proxy_pass最好都带 / ,或者都不带 /;url匹配时,location后面的 / 就是一个普通字符来处理,没有特殊的含义。而proxy_pass中的 / 是资源分割符,是个特殊字符。 区别:location带 / 时,像 /aaaBBB 这种是不会匹配的;location不带 / ,像 /aaaBBB 这种是会匹配的。location带与不带 / 看实际需求。
nginx 的平滑升级
yum安装的不行,得编译安装的。 得先有安装好运行起的一台nginx 然后再安装一台 [root@localhost ~]# tar xzf nginx-1.24.0.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/nginx-1.24.0/ [root@localhost nginx-1.24.0]# /usr/local/nginx/sbin/nginx -t 编译信息得跟已存在的那台相同 [root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream [root@localhost nginx-1.24.0]# make 用make千万不用make install [root@localhost nginx-1.24.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_$(date +%F)(备份原来nginx的nginx并改名以时间结尾) [root@localhost nginx-1.24.0]# cp /usr/local/nginx-1.24.0/objs/nginx /usr/local/nginx/sbin/(拷贝新版本的objs目录下nginx启动文件到旧版本sbin中) [root@localhost nginx-1.24.0]# /usr/local/nginx/sbin/nginx -t(测试有没有问题) 1、给nginx发送平滑迁移信号(若不清楚pid路径,请查看nginx配置文件) [root@localhost ~]# kill -USR2 `cat /var/run/nginx.pid` 2、查看nginx pid,会出现一个nginx.pid.oldbin [root@localhost ~]# ll /var/run/nginx.pid* -rw-r--r--. 1 root root 6 6月 14 19:26 /var/run/nginx.pid -rw-r--r--. 1 root root 6 6月 14 19:16 /var/run/nginx.pid.oldbin 3、从容关闭旧的Nginx进程 [root@nginx-server ~]# kill -WINCH `cat /var/run/nginx.pid.oldbin` 4、此时重载配置启动旧的工作进程 [root@nginx-server ~]# kill -HUP `cat /var/run/nginx.pid.oldbin` 5、结束工作进程,完成此次升级 [root@nginx-server ~]# kill -QUIT `cat /var/run/nginx.pid.oldbin` 6、重新启动新的进程,加载新的配置 kill -HUP `cat /var/run/nginx.pid` 验证Nginx是否升级成功 [root@nginx-server ~]# /usr/local/nginx/sbin/nginx -V **主进程支持的信号** - `TERM`, `INT`: 立刻退出 - `QUIT`: 等待工作进程结束后再退出 - `KILL`: 强制终止进程 - `HUP`: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。 - `USR1`: 重新打开日志文件 - `USR2`: 启动新的主进程,实现热升级 - `WINCH`: 逐步关闭工作进程 **工作进程支持的信号** - `TERM`, `INT`: 立刻退出 - `QUIT`: 等待请求处理结束后再退出 - `USR1`: 重新打开日志文件
错误页面配置
nginx错误页面包括404 403 500 502 503 504等页面,只需要在server中增加以下配置即可: error_page 404 403 500 502 503 504 /404.html; location = /404.html { root /usr/local/nginx/html; } /usr/local/nginx/html/ 路径下必须有404.html这个文件!!!(可以自行网上查找添加相应的)
流量控制
配置基本的限流 “流量限制”配置两个主要的指令,`limit_req_zone`和`limit_req`,(这个两个要对应不然如同虚设) 192.168.200.10上操作 limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; upstream myweb { server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=1; } server { listen 80; server_name localhost; location /login { limit_req zone=mylimit; proxy_pass http://myweb; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 192.168.200.11配置: server { listen 80; server_name localhost; location /login { root /usr/share/nginx/html; index index.html index.html; } } 处理突发,无延迟的排队 limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; upstream myweb { server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=1; } server { listen 80; server_name localhost; location /login { limit_req zone=mylimit burst=20 nodelay; proxy_pass http://myweb; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } Key - 定义应用限制的请求特性。 zone ip缓存数量(1MB=16万个ip地址)Rate - 定义最大请求速率 burst 大于限制的每秒请求将会被放入队列。 nodelay只要在队列中能分配位置,Nginx将立即转发这个请求。 limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; limit_req zone=mylimit burst=20 nodelay;
nginx 变量
可以在sever,http,location等标签中使用set命令(非唯一)声明变量,语法如下 set $变量名 变量值 - nginx 中的变量必须都以$开头 - nginx 的配置文件中所有使用的变量都必须是声明过的,否则 nginx 会无法启动并打印相关异常日志 由于变量是全局可见的所以nginx启动不会报错,而第一个location中并不知道$a的具体值因此返回的响应结果为一个空字符串 location /a/ { return 200 $a; } location /b/ { set $a "hello nginx"; return 200 $a; } 在不同层级的标签中声明的变量性的可见性规则如下: - location标签中声明的变量中对这个location块生效 - server标签中声明的变量对server块以及server块中的所有子块可见 - http标签中声明的变量对http块以及http块中的所有子块可见
构建私有的 CA 机构(假证书)
nginx 性能优化
1、当前系统结构瓶颈
首先需要了解的是当前系统瓶颈,用的是什么,跑的是什么业务。里面的服务是什么样子,每个服务最大支持多少并发。比如针对nginx而言,我们处理静态资源效率最高的瓶颈是多大?能支持多少qps访问请求?怎么得出系统当前的结构瓶颈?
可以通过查看当前cpu负荷,内存使用率,进程使用率来做简单判断。还可以通过操作系统的一些工具来判断当前系统性能瓶颈,如分析对应的日志,查看请求数量。也可以通过nginx http_stub_status_module模块来查看对应的连接数,总握手次数,总请求数。也可以对线上进行压力测试,来了解当前的系统能性能,并发数,做好性能评估。
2、了解业务模式
虽然我们是在做性能优化,但还是要熟悉业务,最终目的都是为业务服务的。我们要了解每一个接口业务类型是什么样的业务,比如电子商务抢购模式,这种情况平时流量会很小,但是到了抢购时间,流量一下子就会猛涨。也要了解系统层级结构,每一层在中间层做的是代理还是动静分离,还是后台进行直接服务。需要我们对业务接入层和系统层次要有一个梳理
3、性能与安全
性能与安全也是一个需要考虑的因素,往往大家注重性能忽略安全或注重安全又忽略性能。比如说我们在设计防火墙时,如果规则过于全面肯定会对性能方面有影响。如果对性能过于注重在安全方面肯定会留下很大隐患。所以大家要评估好两者的关系,把握好两者的孰重孰轻,以及整体的相关性。权衡好对应的点。
4、系统与nginx性能优化
大家对相关的系统瓶颈及现状有了一定的了解之后,就可以根据影响性能方面做一个全体的评估和优化。
-
网络(网络流量、是否有丢包,网络的稳定性都会影响用户请求)
-
系统(系统负载、饱和、内存使用率、系统的稳定性、硬件磁盘是否有损坏)
-
服务(连接优化、内核性能优化、http服务请求优化都可以在nginx中根据业务来进行设置)
-
程序(接口性能、处理请求速度、每个程序的执行效率)
-
数据库、底层服务
上面列举出来每一级都会有关联,也会影响整体性能,这里主要关注的是nginx服务这一层。
1、文件句柄
[root@nginx-server ~]# vim /etc/security/limits.conf #* soft core 0 #* hard rss 10000 #@student hard nproc 20 #@faculty soft nproc 20 #@faculty hard nproc 50 #ftp hard nproc 0 #@student - maxlogins 4 #root只是针对root这个用户来限制,soft只是发提醒,操作系统不会强制限制,一般的站点设置为一万左右就ok了 root soft nofile 65535 root hard nofile 65535 # *代表通配符 所有的用户 * soft nofile 25535 * hard nofile 25535 可以看到`root`和`*`,root代表是root用户,*代表的是所有用户,后面的数字就是文件句柄大小。大家可以根据个人业务来进行设置。
cpu的亲和配置
cpu的亲和能够使nginx对于不同的work工作进程绑定到不同的cpu上面去。就能够减少在work间不断切换cpu,把进程通常不会在处理器之间频繁迁移,进程迁移的频率小,来减少性能损耗。nginx 亲和配置
nginx通用配置优化
#将nginx进程设置为普通用户,为了安全考虑 user nginx; #当前启动的worker进程,官方建议是与系统核心数一致 worker_processes 2; #方式一,就是自动分配绑定 worker_cpu_affinity auto; #日志配置成warn error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; #针对 nginx 句柄的文件限制 worker_rlimit_nofile 65535; #事件模型 events { #使用epoll内核模型,用这个模型来高效处理异步事件 use epoll; #每一个进程可以处理多少个连接,如果是多核可以将连接数调高 worker_processes * 1024 worker_connections 10240; } http { include /etc/nginx/mime.types; default_type application/octet-stream; server_tokens off; #隐藏nginx版本号 charset utf-8; #设置字符集 #设置日志输出格式,根据自己的情况设置 log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$args" "$request_uri"'; access_log /var/log/nginx/access.log main; sendfile on; #对静态资源的处理比较有效 #tcp_nopush on; #如果做静态资源服务器可以打开 #tcp_nodeny on; #当nginx做动态的服务时可以选择打开 keepalive_timeout 65; 该功能避免了建立或者重新建立连接。切记这个参数也不能设置过大!否则会导致许多无效的http连接占据着nginx的连接数,终nginx崩溃! ######## #Gzip module gzip on; #文件压缩默认可以打开 gzip_disable "MSIE [1-6]\."; #对于有些浏览器不能识别压缩,需要过滤如ie6 gzip_http_version 1.1; include /etc/nginx/conf.d/*.conf; }
nginx跨域
跨域(Cross-Origin)是指在 Web 开发中,一个网页的运行脚本试图访问另一个网页的资源时,这两个网页的域名、协议或端口号任何一个不同,就被称为跨域。跨域是由浏览器的同源策略(Same-Origin Policy)所限制的。 同源策略是一种安全机制,它防止一个网页的脚本去读取另一个不同域名的网页内容。同源策略要求两个网页的协议、主机和端口号必须完全相同,否则就会出现跨域问题。简单来说,同源策略要求不同域名的网页之间不能相互访问对方的资源。 跨域问题在前端开发中经常会遇到,比如当一个网页通过 AJAX 请求获取另一个域名下的数据时,由于跨域限制,请求会被浏览器拒绝。为了解决跨域问题,可以通过在服务器端设置相应的跨域资源共享(CORS)策略,或者使用 JSONP、代理服务器等方法来实现跨域请求。 1.Cookie、LocalStorage 和 IndexedDB 的访问限制:如果不使用同源策略,恶意网站可以通过脚本访问其他域名下的 Cookie、LocalStorage 或 IndexedDB 数据,从而获取用户的敏感信息。 2.DOM 访问限制:恶意网站可以通过脚本访问其他域名下的 DOM,执行恶意操作或窃取敏感信息。 3.Ajax 请求限制:跨域的 Ajax 请求可以被恶意网站用来执行 CSRF(跨站请求伪造)攻击,从而以用户身份执行未经授权的操作。 4.跨域资源加载限制:如果不受同源策略限制,恶意网站可以加载其他域名下的资源(如图片、样式表、脚本),可能用于执行 XSS(跨站脚本)攻击或其他类型的攻击。
server { listen 80; server_name example.com; location /api { # 定义代理目标 proxy_pass http://api.example.com; # 允许跨域请求 add_header Access-Control-Allow-Origin *; # 允许带身份验证信息的跨域请求 add_header Access-Control-Allow-Credentials true; # 允许的请求方法 add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; # 允许的请求头 add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; # 预检请求的有效期 add_header Access-Control-Max-Age 3600; # 处理 OPTIONS 请求 if ($request_method = 'OPTIONS') { add_header Content-Type 'text/plain; charset=utf-8'; add_header Content-Length 0; return 204; } } }
负载均衡集群
硬件负载均衡产品:F5 、深信服 、Radware
软件负载均衡产品: LVS(Linux Virtual Server)、 Haproxy、Nginx、Ats(apache traffic server)
标签:index,http,nginx,秘籍,server,Nginx,html,root From: https://blog.csdn.net/m0_64422440/article/details/140693727