一、Nginx是什么?
Nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;同时也是一个IMAP、POP3、SMTP代理服务器;Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。
- Nginx工作流程图:
二、Nginx的重要配置
-
日志
access.log具体每项代表的意思:$remote_addr 客户端地址 211.28.65.253 $remote_user 客户端用户名称 -- $time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800 $request 请求的URI和HTTP协议 "GET /article-10000.html HTTP/1.1" $http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.it300.com 192.168.100.100 $status HTTP请求状态 200 $upstream_status upstream状态 200 $body_bytes_sent 发送给客户端文件内容大小 1547 $http_referer url跳转来源 https://www.baidu.com/ $http_user_agent 用户终端浏览器等信息 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C; $ssl_protocol SSL协议版本 TLSv1 $ssl_cipher 交换数据中的算法 RC4-SHA $upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.10.10.100:80 $request_time 整个请求的总时间 0.205 $upstream_response_time 请求过程中,upstream响应时间 0.002
自己定义log的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" $host' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr" $upstream_response_time'; log_format up_head '$remote_addr - $remote_user [$time_local] $request $host' 'upstream_http_content_type $upstream_http_content_type $upstream_http_host';
-
配置http模块负载均衡
upstream bakend{ server 192.168.81.152:8283; server 192.168.81.153:8283; } server{ listen 8080; access_log /home/myb/wss/nginx/aeecss1.log; error_log /var/log/nginx/error1.log; #proxy_pass bakend; location /get { #匹配 locahost:8080/get http请求转发到152和153的机器上 proxy_pass http://bakend/get } }
-
在stream模块中配置反向代理
stream{ upstream test { server 192.168.81.152:8283; server 192.168.81.152:8283; } server{ listen 8080; #access_log /var/log/nginx/aeecss1.log; #error_log /var/log/nginx/error1.log; proxy_pass test; } }
-
Location规则配置:
location = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何请求,因为所有请求都是以"/"开始 # 但是更长字符匹配或者正则表达式匹配会优先匹配 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location [ configuration C ] } location ~* .(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg结尾的请求. # 但是所有 /images/ 目录的请求将由 [Configuration C]处理. [ configuration D ] }