Nginx是异步框架的网页服务器,也可以用作反向代理、负载平衡器和HTTP缓存。
静态资源web服务器搭建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| server { listen 80; server_name localhost;
location / { alias html; index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
}
|
主要配置server,location 指定对应的文件目录,建议使用alias,使用root,会将文件路径混入到文件路径中,开启gizp,可以减少传输文件大小
1 2 3 4
| gip on; gipz_min_length 1; gipz_comp_level 2; gizp_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png
|
静态文件服务器
1 2 3 4 5 6 7 8 9 10
| server{
... alias html; autoindex on; set $limit_rate 1k; ... }
|
access日志格式
反向代理服务器
1 2 3 4 5 6 7 8 9 10 11 12 13
| server { server_name localhost; listen 80
location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Cookie $http_cookie; }
}
|
缓存服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m user_temp_path=off
server { server_name localhost; listen 80
location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Cookie $http_cookie;
proxy_cache my_cache; proxy_cache_key $host$uri$is_args$args; proxy_cache_valid 200 304 302 1d; }
}
|