-------------------- 写在这是方便你们找php
nginx的安装配置三部曲:点击便可查看css
一、安装和配置文件的基础配置,ctrl+f搜索nginxhtml
二、nginx文档如何查看 + 经常使用的配置方式(伪静态,反向代理,动静分离,防盗链,图片缓存,gzip图片压缩)linux
三、高级配置-》负载均衡nginx
---------------------web
Nginx从新读取配置的命令swift
nginx -s reload
后端
gzip压缩文件模块的使用:缓存
参考:nginx官方文档-》Modules reference-》ngx_http_gzip_moduletomcat
语法规则: location [=|~|~*|^~] /uri/ { … }
=
开头表示精确匹配
^~
开头表示uri以某个常规字符串开头,理解为匹配 url路径便可。nginx不对url作编码,所以请求为/static/20%/aa,能够被规则^~ /static/ /aa匹配到(注意是空格)。以xx开头
~
开头表示区分大小写的正则匹配 以xx结尾
~*
开头表示不区分大小写的正则匹配 以xx结尾
!~
和!~*
分别为区分大小写不匹配及不区分大小写不匹配 的正则
/
通用匹配,任何请求都会匹配到。
多个location配置的状况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,没必要拘泥,仅供参考):
首先精确匹配 =-》其次以xx开头匹配^~-》而后是按文件中顺序的正则匹配-》最后是交给 / 通用匹配。
当有匹配成功时候,中止匹配,按当前匹配规则处理请求。
例子,有以下匹配规则:
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D,注意:是根据括号内的大小写进行匹配。括号内全是小写,只匹配小写
}
location ~* \.png$ {
#规则E
}
location !~ \.xhtml$ {
#规则F
}
location !~* \.xhtml$ {
#规则G
}
location / {
#规则H
}
那么产生的效果以下:
访问根目录/, 好比http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,可是规则D顺序优先,规则E不起做用, 而 http://localhost/static/c.png 则优先匹配到 规则C
访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,由于规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,
http://localhost/a.XHTML不会匹配规则G,(由于!)。规则F,规则G属于排除法,符合匹配规则也不会匹配到,因此想一想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,由于以上规则都不匹配,这个时候nginx转发请求给后端应用服务器,好比FastCGI(php),tomcat(jsp),nginx做为方向代理服务器存在。
因此实际使用中,我的以为至少有三个匹配规则定义,以下:
#直接匹配网站根,经过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。 #这里是直接转发给后端应用服务器了,也能够是一个静态首页 # 第一个必选规则 location = / { proxy_pass http://tomcat:8080/index } # 第二个必选规则是处理静态文件请求,这是nginx做为http服务器的强项 # 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用 location ^~ /static/ { //以xx开头 root /webroot/static/; } location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { //以xx结尾 root /webroot/res/; } #第三个规则就是通用规则,用来转发动态请求到后端应用服务器 #非静态文件请求就默认是动态请求,本身根据实际把握 location / { proxy_pass http://tomcat:8080/ }
nginx的其余配置信息介绍
3、ReWrite语法
last
– 基本上都用这个Flag。break
– 停止Rewirte,不在继续匹配redirect
– 返回临时重定向的HTTP状态302permanent
– 返回永久重定向的HTTP状态301
一、下面是能够用来判断的表达式:
-f
和!-f
用来判断是否存在文件-d
和!-d
用来判断是否存在目录-e
和!-e
用来判断是否存在文件或目录-x
和!-x
用来判断文件是否可执行
二、下面是能够用做判断的全局变量
例:http://localhost:88/test1/test2/test.php
$host:localhost $server_port:88 $request_uri:http://localhost:88/test1/test2/test.php $document_uri:/test1/test2/test.php $document_root:D:\nginx/html $request_filename:D:\nginx/html/test1/test2/test.php
附:一些可用的全局变量
$args $content_length $content_type $document_root $document_uri $host $http_user_agent $http_cookie $limit_rate $request_body_file $request_method $remote_addr $remote_port $remote_user $request_filename $request_uri $query
一、普通的(静态的)http服务器
这样若是访问http://localhost 就会默认访问到E盘wwwroot目录下面的index.html,若是一个网站只是静态页面的话,那么就能够经过这种方式来实现部署。
-
server {
-
listen
80;
-
server_name localhost;
-
client_max_body_size
1024M;
-
-
-
location / {
-
root e:wwwroot;
//思路:经过/将全部的请求,转发给root处理
-
index index.html;
-
}
-
}
二、反向代理
localhost的时候,就至关于访问localhost:8080了
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port; //思路:经过/,将全部的请求,转发给第3方处理
}
}
既然服务器能够直接HTTP访问,为何要在中间加上一个反向代理,不是画蛇添足吗?反向代理有什么做用?
负载均衡、虚拟主机等,都基于反向代理实现,固然反向代理的功能也不只仅是这些。
三、Redirect(重定向)语法
server { listen 80; server_name start.igrow.cn; index index.html index.php; root html; if ($http_host !~ "^star\.igrow\.cn$" { rewrite ^(.*) http://star.igrow.cn$1 redirect; } }
四、防盗链
location ~* \.(gif|jpg|png|bmp)$ {
valid_referers none blocked *.ttlsa.com server_names ~\.google\. ~\.baidu\.;
if ($invalid_referer) {
return 403;
#rewrite ^/ http://www.ttlsa.com/403.jpg;
}
}
五、根据文件类型设置过时时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ { if (-f $request_filename) { //只能是文件,由于这用-f判断了 expires 1h; break; } }
六、设置图片缓存(过时)时间
七、禁止访问某个目录
location ~* \.(txt|doc)${ root /data/www/wwwroot/linuxtone/test; #全部用户都禁止访问这个目录 deny all; }
八、隐藏版本号的做用
经过你所用的版本,找其漏洞,进行攻击你
在http中添加该配置:server_tokens off;
九、配置https
一、去阿里云/腾讯云申请免费的
二、下载证书
三、证书放到/usr/local/nginx目录下(就是和conf同级,nginx.conf默认的配置文件的上一级)
四、在vhost目录下加入配置文件
server {
listen 443;
server_name lampol.edu0532.cn; #改域名
ssl on;
root /home/www/xcxtp5/public; #改项目路径
ssl_certificate ../certbo/1523694051089.pem; #改证书路径
ssl_certificate_key ../certbo/1523694051089.key; #改私钥路径
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
index index.html index.htm index.php;
autoindex on;
# 伪静态配置
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
十、动静分离
思路:动、静态的文件,请求时匹配不一样的目录
当访问gif,jpeg时 直接访问e:wwwroot;
,正则自行配置
server {
listen 80;
server_name localhost;
location / {
root e:wwwroot;
index index.html;
}
# 全部静态请求都由nginx处理,存放目录为html
location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
root e:wwwroot;
}
# 全部动态请求都转发给tomcat处理
location ~ .(jsp|do)$ {
proxy_pass http://test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html { root e:wwwroot; } }
参考:nginx官方文档-》Modules reference-》ngx_http_upstream_module