一、nginx在应用程序中的做用:
- 解决跨域
- 请求过滤
- 配置gzip
- 负载均衡
- 静态资源服务器
二、基本配置
events {
}
http {
server {
listen 8080,
server_name: www.baidu.com;
location / (请求路径) {
proxy_pass http:
}
location = *.html {
root G:/My_Project/Server/index.html;
}
location / {
rewrite ^.*$ / index.html redirect;
}
}
server {
listen: 9001;
server_name: xxx.com;
location /api {
proxy_pass http:
}
}
upstream balanceServer {
least_conn;
server 10.132.28.29:3030;
server 10.132.28.30:3030;
server 10.132.28.31:3030;
server 10.132.28.32:3030;
}
}
gzip on;
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_min_length 1000;
gzip_types text/csv text/xml text/css text/plain text/javascript application/javascript
application/x-javascript application/json application/xml;
复制代码
三、内置全局变量
变量名 |
做用 |
$host |
请求信息中的Host |
$request_method |
请求类型 |
$remote_addr |
客户端的IP地址 |
$args |
请求参数 |
$http_cookie |
客户端cookie信息 |
$remote_port |
客户端端口 |
$server_addr |
服务端地址 |
$server_name |
服务端名称 |
$server_port |
服务端端口 |
四、负载均衡策略
- 轮询策略(默认): 其中一台服务器出现延迟,影响全部分配到此服务器的用户;
- 最小链接数策略(least_conn): 优先分配给压力较小的服务器;
- 最快响应时间策略(fair):优先分配给响应时间最短的;
- 客户端ip绑定(ip_hash): 同一个ip的请求只分配给一台服务器,解决网页session共享问题。