nginx隐藏版本信息
server_tokens off;
1.rewrite介绍以及相关指令
url重写
rewrite相关指令 分为重定向、if语句、条件判断
1.重定向:rewrite 将访问的url 更换成指定的文件
2. if语句 应用环境: server location
语法: if ( condition ) {...}
3.条件判断
~* 正则匹配不区分大小写
!~ 非正则匹配(区分大小写)
!~* 非正则匹配(不分区大小写)
-f和!-f 用来判断是否存在文件
-d和!-d 用来判断是否存在目录
-e和!-e 用来判断是否存在文件或则目录
-x和!-x 用来这个文件是否可执行
2.rewrite示例
例1:
目的:当前用户访问http://192.168.100.10/abc/a/1.html地址时,通过 redirect 重定向至 http://192.168.100.10/ccc/bbb/2.html
location /abc {
rewrite 原来的url 新的url;
rewrite .* /ccc/bbb/2.html;
}
这个访问url不会变,url还是原本的http://192.168.100.10/abc/a/1.html ,但是网页的内容就变成http://192.168.100.10/ccc/bbb/2.html的内容了
如果希望url和内容都改变,则需要在 rewrite 的结尾添加permanent
location /abc {
rewrite 原来的url 新的url;
rewrite .* /ccc/bbb/2.html permanent;
}
permanent 代表永久的
添加 url和内容都会改变--> url被替换,实际产生了两次请求
不添加 url不会变,但内容会变
如
location = /abc =代表精确匹配
location ~ /abc 正则匹配,模糊匹配,主要你输入的url包含这个内容就要可以部分匹配
问:
当用户访问url http://192.168.100.10/ade/abc/1.html 是否会以这样的语句匹配(location ~ /abc)匹配
答:会匹配 只要带abc就行
例2
目的:将http://192.168.100.10/2016/a/b/c/1.html 换成 http://192.168.100.10/2017/a/b/c/1.html
location /2016 {
rewrite ^/2016/(.*)$ /2017/$1 permanent;
}
利用正则中的()和$1
(.*)$代表2016后面不管还有什么后缀都用$1来接收,来确保2016和2017后面的url时一致的
注:如果测试,不需要准备2016的文件,因为是不会访问2016的,只需要准备2017的文件就行,准备跳转后的网页
例3
目的:当用户访问 http://www.xp.com 换成 http://jd.com
if ( $host ~* xp.com ) {
rewrite .* http://jd.com;
}
$host 主机地址
~* 不区分大小写
例:cloud.com/ccc/bbb/2.html
其中 cloud.com 叫host
/ccc/bbb/2.html 叫 request uri
例4
目的:无论输入的url的页面时什么 http://xp.com/1.html http://xp.com/2.html 其结果全部都重定向至 http://cloud.com/1.html http://cloud.com/2.html 把主机地址由原来的 xp.com 换成 cloud.com,页面内容不变
vim /etc/nginx/cond.d/cloud.com.conf
server {
listen: 80;
server_name cloud.com;
}
location / {
root /cloud;
index index.html
}
vim /etc/nginx/nginx.conf
server {
if ( $host ~* xp.com ) {
rewrite .* http://cloud.com$request_uri;
}
}
其中 $request_uri; 是用户自己打的
例5
目的:在访问url是目录时,在url的结尾自动加一个 / 如果不是目录则不加,先做一个判断,是目录才需要加,不是目录则不需要加 /
当用户访问时,输入的url不完整
1.输入的url时目录时,自动添加 /
http://www.baidu.com/home
2.输入的url时文件时,不添加添加 /
http://www.baidu.com/home/index.html
3.输入的url时目录时,但结尾已经添加 / 时,不添加 /
http://www.baidu.com/home/
server {
if ( -d $request_filename ) {
rewrite .* http://$host$request_uri/
rewrite ^(.*)([^/])$ http://$host$!$2/;
$1 $2
}
}
$request_filename 只判断最后一个 / 后面的内容
例6
目的:将url中的字段,引入重定向后新的url中 http://www.xp.com/login/xp.html 转为 http://www.xp.com/reg/login.php?use1:xp
location /login {
rewrite ^/login/(.*)\.html$ /reg/login.php?user:$1;
}
$1代表前面括号里面的内容
例7
目的:原先的 "_" 分割,转换成 "/" 目录层次
将 http://www.xp.com/wyc/11-22-33/1.html 转换成 http://www.xp.com/wyc/11/22/33/1.html
location /wyc {
rewrite ^/wyc/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /wyc/$1/$2/$3$4 permanent;
}
[0-9] 匹配0-9都被匹配为一个整体
+ 表示匹配一次或多次
(.*)$ 不管用户输入的时什么都将匹配为第四个整体
例8
目的:将原先url中的信息 重定向至新的url中
http://alice.xp.com --> http://www.xp.com/alice
http://jack.xp.com --> http://www.xp.com/jack
if ( $host ~* "^www.xp.com$" ) {
break;
}
if ( $host ~* "^(.*)\.xp.com\.com$" ) {
\\这是转义符
定义变量指令 set $user $1; #等同于shell中的user=123
rewrite .* http://www.xp.com/$user permanent;
}
例9
目的:如果访问服务器中的特殊文件,如 .sh 结尾的文件,则返回403
location ~* \.sh$ {
return 403; 或 return 301 http://www.xp.com;
}
\.sh 其中 \ 代表转义符
3.代理跳转实例
例:由1.html跳转到2.html
server {
listen 8008;
server_name 192.168.193.204;
location / {
rewrite /1.html http://192.168.193.204:8008/2.html permanent;
root /usr/share/nginx/html;
index index.html;
}
}
在浏览器访问 http://192.168.193.204:8008/1.html 就会自动跳转到192.168.193.204:8008/2.html
反向代理跳转
server {
listen 8008;
server_name 192.168.293.206;
location /xp {
rewrite ^/xp http://192.168.193.205:8008/2.html permanent;
# proxy_pass http://192.168.211.25/;
root /usr/share/nginx/html;
index index.html;
}
}
在浏览器访问 http://192.168.293.206:8008/xp 就会自动跳转到192.168.193.205:8008/2.html
4.
nginx在做反向代理到后端服务器,如果后端服务器有重定向,会出现返回服务器的ip地址,解决办法:
server {
listen 80;
server_name www.boke.com;
location / {
proxy_pass http://192.168.1.100:8080;
#增加下面配置进行重定向到nginx的ip或者域名
proxy_redirect http://192.168.1.100:8080/ http://www.baas.com/;
}
}
proxy_redirect
语法:proxy_redirect [ default|off|redirect replacement ]
默认值:proxy_redirect default
使用字段:http, server, location
如果需要修改从被代理服务器传来的应答头中的"Location"和"Refresh"字段,可以用这个指令设置。
5.break 和 last 的区别
server {
listen 80;
location / {
root /use/share/nginx/html;
index index.html index.php;
}
location /break {
rewrite .* /test/break.html break; 重定向之后,将停止后面的所有匹配,一般来说 nginx 匹配从上至下,当匹配到break之后,就不继续往下了,直接结束
root /usr/share/nginx/html;
}
location /last {
rewrite .* /test/last.html last; 当匹配到这个url之后,直接跳过,直接去匹配下一个,暂时隐藏
root /usr/share/nginx/html;
}
}
https介绍
http+ssl
安全套接字
加密算法分为
1.对称
1.1 AES
1.2 DES
1.3 3DES
2.非对称
2.1 DH
2.2 RSA
3.HASH
3.1 MD5
私有CA
1..生成证书以及密钥文件
1.1 准备有效证书文件
mkdir -p /etc/nginx/ssl
cd /etc/nginx/ssl
1.2 生成私钥
使用openssl生成基于rsa数学算法长度 1024bit 的密钥,文件须以key结尾
openssl genrsa -out server.key 2048
解释
/etc/nginx/ssl/server.key 生成的 key 存放在这里
2048 密钥长度 密钥长度还有 512 128 256 1024
1.3使用密钥文件生成证书--申请书
openssl req -new -key server.key -out server.csr
说明:
1)程序在生成的过程中需要填写说明:
2)Organizaiton Name: 填写公司名称。
3)Common Name: 必须填写与实际使用https的网站域名吻合。我这边是xp.com 否则会引起浏览器警告。
1.4 同意申请,生成证书 (把这两个证书合起来就能用)
[root@test4 ssl]# openssl x509 -req -days 30 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=CN/ST=jiangsu/L=nanjing/O=zonghui/OU=IT/CN=xp.com
参数解释
-x509 证书格式,固定的
days 证书有效期
key 指定密钥文件
in 指定证书申请文件
2.私有 CA 的https部署
1.创建目录
mkdir /xp
echo 'cdvvvevav' > /xp/index.html
2.在 /etc/hosts 下做域名解析
3.更改配置文件
vim /etc/nginx/conf.d/xp.conf
server {
listen 80;
server_name www.xp.com;
location / {
root /xp
index index.html;
}
}
3.你会发现此时还是http访问
4.如何用 https 访问
server {
listen 443 ssl;
server_name www.xp.com;
ssl on; #开启SSL
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_prefer_server_ciphers on; #优先使用服务端的SSL密钥证书,默认为off
location / {
root /xp;
index index.html;
}
}
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
路径可以自定义