一、nginx概念(反向代理,负载均衡,动静分离)
1)反向代理
2)负载均衡
3)动静分离
二、default.conf文件配置
1)默认写法
server { listen 80; listen [::]:80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
2)静态文件服务器*
server { listen 80; location / { root /usr/share/nginx/html; index index.html index.htm; } location /static { alias /data; autoindex on; } }
3)负载均衡
upstream testServe { server 127.0.0.1:8000 weight=3; server 127.0.0.1:8001; server 127.0.0.1:8002; } server { listen 80; localhost / { proxy_pass http://testServe; } }
4)修改请求信息,转发到目标路径
server { listen 8080; location / { proxy_pass http://42.192.139.14:8080; } location /rewrite { if ($uri = /rewrite) { rewrite /rewrite http://42.192.139.14:8080 break; } rewrite /rewrite/([^/]*)/(.*) http://42.192.139.14:8080/$1/$2 break; } }