# 概念
- 是什么,可以做什么
- 反向代理
- 负载均衡
- 动静分离
# 安装使用,配置
- linux中安装
- 常用命令
- 查看nginx版本号
./nginx -v
- 关闭:
./nginx -s stop
- 启动:
./nginx
- 热加载:
./nginx -s reload
- 检查配置文件:
./nginx -t
- 配置文件
- 组成部分:
- 全局块:
- events块:
- http块:
# 全局块 --------------------------------------------------------------------------------start #user nobody; # 越大并发数量越多 worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; # 全局块 --------------------------------------------------------------------------------end # events块 ------------------------------------------------------------------------------start events { worker_connections 1024; # 支持最大的连接数 } # events块 ------------------------------------------------------------------------------end # http块 ------------------------------------------------------------------------------start http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } # http块 ------------------------------------------------------------------------------end
- location
# 配置实例
- 反向代理
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # 相当于是匹配路径 location ~ /server1/ { proxy_pass http://localhost:9701; } # ~ 表示是正则 location ~ /server2/ { proxy_pass http://localhost:9702; }
- 负载均衡
- 分配策略:
- 轮询(默认), 会自动剔除无效的服务
- 权重
weight
, 默认是1 ip_hash
,可保证同一个ip(用户)一直访问同一个服务器fair
,根据服务响应时间来分配
http { # 负载均衡服务 upstream blance_server{ # ip_hash的方式分配请求 #ip_hash; # 根据服务响应时间来分配 # fair # 权重的方式分配请求 server localhost:9701 weight=1; server localhost:9702 weight=2; } server { listen 80; server_name localhost; location ~ /blance/ { proxy_pass http://blance_server; proxy_connect_timeout 10; } } }
- 动静分离
提高访问效率
高可用集群
- https://www.cnblogs.com/jiangwangxiang/p/8481661.html
- https://www.gulixueyuan.com/course/332/task/9928/show
# 原理