简单介绍nginx的使用
一、最简配置
首先nginx的配置文件是nginx.conf,存在于nginx/conf文件夹下
然后说一下里面的内容
worker_processes 1; # 设置多少个进程,与cup核保持一致最好events {worker_connections 1024; # 每个进程允许的最大连接数}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server{listen 8080;server_name localhost;location / {root D:/STUDY; # 当输入[server_name]:[listen] 时会进入这个目录index a.html; # 默认界面}error_page 404 /error.html; #根据错误码跳转到error 界面}include vhosts/*.conf;}
还可以在http最后面加上这句 include vhosts/*.conf; 这个是说把vhost文件夹里的所有.conf引入,引入的.conf文件里面写的是server{}
二、高级配置
1.日志配置
log_format设置日志格式,使用方法:
log_format 格式名 '格式内容';
使用方法
access_log 存储路径 格式名;
log_format (日志的格式化):
'$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"'
log_fomat 为日志输出格式
remote_user:认证的用户名,没有开启不会显示
time_local :nginx 服务器时间
status :状态码
body_bytes_sent :服务端响应给客户端的body信息大小
http_user_agent:客户端使用代理(比如使用什么访问的)
http_x_forwarded_for :每一级请求所携带的http 信息
下面为一段输出的日志,可以跟上述的format 对比一下
127.0.0.1 - - [30/Dec/2018:14:02:03 +0800] "GET /aa HTTP/1.1" 404 51 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" "-"127.0.0.1 - - [30/Dec/2018:14:11:31 +0800] "GET /aa HTTP/1.1" 404 51 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" "-"
2.location配置
Nginx的HTTP配置主要包括三个区块,结构如下:http { //这个是协议级别include mime.types;default_type application/octet-stream;keepalive_timeout 65;gzip on;server { //这个是服务器级别listen 80;server_name localhost;location / { //这个是请求级别root html;index index.html index.htm;}}}
location区段
通过指定模式来与客户端请求的URI相匹配,基本语法如下:location [=|~|~*|^~|@] pattern{……}
a、没有修饰符 表示:必须以指定模式开始,如:
server {server_name baidu.com;location /abc {……}}那么,如下是对的:http://baidu.com/abchttp://baidu.com/abc?p1http://baidu.com/abc/http://baidu.com/abcde
b、=表示:必须与指定的模式精确匹配
server {server_name sishlocation = /abc {……}}那么,如下是对的:http://baidu.com/abchttp://baidu.com/abc?p1如下是错的:http://baidu.com/abc/http://baidu.com/abcde
c、~ 表示:指定的正则表达式要区分大小写
server {server_name baidu.com;location ~ ^/abc$ {……}}那么,如下是对的:http://baidu.com/abchttp://baidu.com/abc?p1=11&p2=22如下是错的:http://baidu.com/ABChttp://baidu.com/abc/http://baidu.com/abcde
d、~* 表示:指定的正则表达式不区分大小写
server {server_name baidu.com;location ~* ^/abc$ {……}}那么,如下是对的:http://baidu.com/abchttp://baidu..com/ABChttp://baidu..com/abc?p1=11&p2=22如下是错的:http://baidu..com/abc/http://baidu..com/abcde
e、^~ 类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,
那么就停止搜索其他模式了。
f、@ :定义命名location区段,这些区段客户段不能访问,只可以由内部产生的请
求来访问,如try_files或error_page等
查找顺序和优先级
1:带有“=“的精确匹配优先
2:没有修饰符的精确匹配
3:正则表达式按照他们在配置文件中定义的顺序
4:带有“^~”修饰符的,开头匹配
5:带有“~” 或“~*” 修饰符的,如果正则表达式与URI匹配
6:没有修饰符的,如果指定字符串与URI开头匹配
Location区段匹配示例location = / {# 只匹配 / 的查询.[ configuration A ]}location / {# 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。[ configuration B ]}location ^~ /images/ {# 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。[ configuration C ]}location ~* \.(gif|jpg|jpeg)$ {# 匹配任何以gif, jpg, or jpeg结尾的文件,但是所有 /images/ 目录的请求将在Configuration C中处理。[ configuration D ]} 各请求的处理如下例:■/ → configuration A■/documents/document.html → configuration B■/images/1.gif → configuration C■/documents/1.jpg → configuration D
root 、alias指令区别
location /img/ {alias /var/www/image/;}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会去/var/www/image/目录找文件
location /img/ {root /var/www/image;}
若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。而root则可有可无
3.设置gzip提升网站运行速度
改成这样就可以了
gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_comp_level 1;gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)gzip_vary on;
4.负载均衡的配置
负载均衡就是为了不让所有的请求都访问一台服务器
域名绑定在负载均衡器上
需要在nginx负载均衡器上完成配置
a、配置连接池,也就是转发的web服务器,语法:
upstream 连接池名称 {server 192.168.0.100;server .............}
b、给负载均衡器绑定域名,建立一个虚拟主机,在虚拟主机里配置指向连接池的名字
http {upstream dalaoyang-server {server localhost:10001;server localhost:10002;}server {listen 10000;server_name localhost;location / {proxy_pass http://dalaoyang-server;proxy_set_header X-Forwarded-For $remote_addr;proxy_set_header Host $host;}}}
4.重定向
例如:
server { # 添加个server区块做跳转listen 80;server_name brian.com;rewrite ^/(.*) http://www.brian.com/$1 permanent;}server {listen 80;server_name www.brian.com;location / {root html/brian;index index.html index.htm;}access_log logs/brian.log main gzip buffer=128k flush=5s;error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
301永久重定向 rewrite ^/(.*) http://目标域名 permanent;
302临时重定向 rewrite ^/(.*) http://目标域名 redirect;
last重定向域名不变内容变(页面跳转到另一个页面)
last: rewrite ^/index\.php /abc.htmk last;
break重定向域名改变内容改变(域名到另一个域名)
break: rewrite ^/(.*) http://www.brian.com/$1 break;
