nginx高级用法

功能 说明 配置语法 配置位置 配置举例 结果验证 备注
rewrite

跳转重定向
(不同于代理的跳转重定向,此处nginx不是代理服务器,而是本身就是web服务器)

rewrite 正则表达式 replacement[flag] server、location、if一级来配置

1、
location /down {
rewrite ^/down http://www.cctv.com permanent;
}

2、
location / {
rewrite ^/down /test/abc.html permanent;
root /opt/work;
}

1、访问http://Nginx地址/down时将跳转至http://www.cctv.com
2、访问http://Nginx地址/down时将跳转至http://Nginx地址/test/abc.html

正则表达式中()用于匹配括号之间的内容,通过$1,$2调用

flag标志位:
last:停止rewrite检测
break:停止rewrite检测
redirect:返回302临时重定向,地址栏会显示跳转后的地址
permanent:重返301永久重定向,地址栏会显示跳转后的地址(浏览器会永远记住,即使nginx服务器关闭了也还是会跳转至其他网页)(IE是这样的,但是搜狗浏览器收到301依然当做临时重定向处理)

rewrite加在不同位置时的优先级规则:server > location

SSL  提供HTTPS服务

ssl on|off
ssl_certificate file
ssl_certificate_key file

http、server这一级来配置

server{
listen 443;
server_name hk.com;

keepalive_timeout 100; ===>优化之使用长连接,100s

ssl on;
ssl_certificate /etc/nginx/ssl_key/hk1.crt;
ssl_certificate_key /etc/nginx/ssl_key/hk.key;

扫描二维码关注公众号,回复: 6259884 查看本文章

ssl_session_timeout 10m; ===>优化之SSL会话过期,10分钟
ssl_session_cache shared:SSL:10m; ===>优化之使用SSL缓存,大小为10M,可以存储大约8k到10k的会话
location / {
root /opt/app/code;
index index.html index.htm;
}
}

 

证书生成步骤(测试使用,生产环境肯定需要向权威CA厂商购买证书):

一、生成秘钥
openssl genrsa -des3 -out hk.key 1024
-des3:选择3des算法
1024:加密位数

二、生成证书
openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout hk.key -out hk1.crt
-days:证书有效期
-x509:选择hash算法
rsa:2048:选择2048位加密
-keyout:key文件
-out:输出结果


苹果终端对服务端的要求:
1、服务器所有的连接使用TLS1.2以上的版本(openssl 1.0.2)
2、HTTPS证书必须使用SHA256以上的哈希算法签名
3、HTTPS证书必须使用RSA 2048位或ECC 256位以上公钥算法
4、使用前向加密技术

try_files 相当于if语句(如果这个路径下找不到则匹配另一个location) try_files $uri @其他location location一级来配置

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri @code1;
}
location @code1 {
proxy_pass http://www.cctv.com;
}

此时访问http://nginx服务器/1.html时会先去/usr/share/nginx/html/目录找,
如果这个目录没有目标文件则匹配 @code1 这个location

测试时候发现location里面有try_files后index语句将失效,即访问http://nginx服务器/时
也会被导向@code1

 worker_processes  制定nginx的work进程数(不包含manage进程)  worker_processes 进程数  最开头那级来配置  worker_processes 10    建议和物理CPU核心数相同
 worker_rlimit_nofile   修改nginx最大句柄  worker_rlimit_nofile 句柄数  最开头那级来配置,和worker_processes  1;一级  worker_rlimit_nofile 65535;    

文件句柄的理解:程序打开一个本地文件产生一个文件句柄,默认操作系统只允许一个应用程序打开最多1024个文件

感觉只有nginx做web服务器时才需要调文件句柄,因为做反向代理的时候也不总是打开本地文件啊

linux修改文件句柄最大数(默认为1024):
vi /etc/security/limits.conf
root soft nofile 65535
root hard nofile 65535
* soft nofile 25535
* hard nofile 25535

CPU亲和 将所有进程均匀的分布在各个cpu核心上 worker_cpu_affinity auto; 最开头那级来配置,和worker_processes  1;一级

user nginx;
worker_processes 24;
worker_cpu_affinity auto;

[root@localhost conf.d]# ps -eo pid,args,psr | grep [n]ginx
9062 nginx: master process nginx 19
9371 nginx: worker process 0
9372 nginx: worker process 1
9373 nginx: worker process 2
9374 nginx: worker process 3
9375 nginx: worker process 4
9376 nginx: worker process 5
9377 nginx: worker process 6
9378 nginx: worker process 7
9379 nginx: worker process 8
9380 nginx: worker process 9
9381 nginx: worker process 10
9382 nginx: worker process 11
9383 nginx: worker process 12
9384 nginx: worker process 13
9385 nginx: worker process 14
9386 nginx: worker process 15
9387 nginx: worker process 16
9388 nginx: worker process 17
9389 nginx: worker process 18
9390 nginx: worker process 19
9391 nginx: worker process 20
9392 nginx: worker process 21
9393 nginx: worker process 22
9394 nginx: worker process 23

没有配置worker_cpu_affinity auto;时的CPU占用情况:
[root@localhost conf.d]# ps -eo pid,args,psr | grep [n]ginx
9062 nginx: master process nginx 22
9406 nginx: worker process 5
9407 nginx: worker process 0
9408 nginx: worker process 9
9409 nginx: worker process 2
9410 nginx: worker process 1
9411 nginx: worker process 3
9412 nginx: worker process 11
9413 nginx: worker process 0
9414 nginx: worker process 12
9415 nginx: worker process 6
9416 nginx: worker process 2
9417 nginx: worker process 5
9418 nginx: worker process 15
9419 nginx: worker process 9
9420 nginx: worker process 17
9421 nginx: worker process 0
9422 nginx: worker process 7
9423 nginx: worker process 2
9424 nginx: worker process 8
9425 nginx: worker process 9
9426 nginx: worker process 5
9427 nginx: worker process 0
9428 nginx: worker process 6
9429 nginx: worker process 2

猜你喜欢

转载自www.cnblogs.com/baihualin/p/10897030.html