技术实践

Nginx常用配置

开启浏览器缓存后,在缓存期内,访问同一资源不会再向服务器发送请求,而是直接从客户端缓存里面拉取资源。 开启服务器缓存后(假设客户端缓存已过

羊先生的头像
羊先生2020.03.17 · 3 分钟阅读 · 158 阅读
Nginx常用配置封面
文章正文还需 3 分钟

gzip压缩

nginx 复制代码
# 开启gzip
gzip on;
# 设置HTTP协议版本
gzip_http_version1.1;
# 压缩级别
gzip_comp_level2;
#文件大于20k时才压缩
gzip_min_length20k;
# 设置要压缩的文件类型
gzip_typestext/plain text/css
application/x-javascript text/xml
application/xml application/xml+rss
text/javascript application/javascript; 

参数说明

gzip:打开或关闭gzip(on/off)

gzip_http_version:http协议版本

gzip_comp_level:压缩级别

gzip_types:需要压缩的文件版本

gzip_min_length:文件内容大于这个值时才会被压缩

缓存

客户端缓存:cache-control、expires

服务器缓存:last-modified、etag

开启浏览器缓存后,在缓存期内,访问同一资源不会再向服务器发送请求,而是直接从客户端缓存里面拉取资源。

开启服务器缓存后(假设客户端缓存已过期),客户端请求同一资源时,会向服务器发送一个请求,服务器判断资源是否更新,如果没有更新,则返回304告诉客户端从本地缓存里面读取资源。

服务器缓存默认是开启的。

nginx 复制代码
# 设置客户端缓存 30s
add_header Cache-Control max-age=30;

# 设置客户端缓存30s
expires  30s;

# 关闭last-modified
add_header Last-Modified"";

# 关闭etag
etagoff;

include

每个服务应该单独配置,再通过include加载到主配置文件里面。

nginx 复制代码
include /etc/nginx/conf.d/*.conf;

重定项 (rewrite)

永久重定项:301

临时重定项:302

nginx 复制代码
# 301
rewrite ^/apm/(.*) /api/$1 permanent;

# 302
rewrite ^/apn/(.*) /api/$1 redirect;

# https 重定向到http
rewrite ^(.*)$  http://$host$1 permanent;

rewrite  "^/(.*)$"  http://blog.qianyu.cn/$1 break;

server

server

nginx 复制代码
# 兼听的端口
listen 80;

# 服务域名
server_name www.webzhong.com;

# 请求成功的log日志
access_log /home/www/log/nginx/access.log main;

# 请求失败的log日志
error_log /home/www/log/nginx/error.log debug;

# 路由配置
location /{
    root /home/www/blog/;
    index index.html;
}

location ~* /api/ {
    expire 30d;
    proxy_pass      http://45.77.1.2:8001;
    add_header  Content-Length 50;
}

负载均衡(LoadBalance)

nginx 复制代码
# weight:权重,值越大越优先转发
# max_fails:最大失败次数,当达到这个次数,ng会把这个节点标记为不可用
# fail_timeout:超时时长
upstream lb-test {
oserver 127.0.0.1:8001 weight=1 max_fails=3 fail_timeout=10s;
oserver 127.0.0.1:8002 weight=1 max_fails=3 fail_timeout=10s;
}
server {
olisten 80;
oserver_name webzhong.com;

olocation / {
                proxy_pass      http://lb-test;
                proxy_set_header  Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

以下是8001和8002端口的配置

nginx 复制代码
server {
    listen 8001;
    server_name 127.0.0.1;

    access_log /home/www/log/nginx/lb1-access.log main;
    error_log /home/www/log/nginx/lb1-error.log debug;

    location  / {
            proxy_pass      http://45.77.1.1:8801;
    }
    olocation ~* /.well-known {
    	oroot /home/www/blog/;
    }
}
server {
    listen 8002;
    server_name 127.0.0.1;

    access_log /home/www/log/nginx/lb1-access.log main;
    error_log /home/www/log/nginx/lb1-error.log debug;

    location  / {
            proxy_pass      http://45.77.1.1:8801;
    }

    olocation ~* /.well-known {
	oroot /home/www/blog/;
    }
}

Nginx常用变量

nginx 复制代码
#是否有参数
$is_args

# 请求的uri
$request_uri

# 请求参数
$args

# 请求host
$host

# 客户端ip
$remote_addr

# 客户端端口
$remote_port