常用的nginx配置
下面的配置文件加载在nginx的配置文件nginx.conf里的http字段里
1.http配置
server {
listen 80; #监听端口
server_name example.com; #域名或本地服务器ip
#add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'unsafe-inline'; img-src 'self' data: http: https: blob:; connect-src 'self' http: https: wss: ws: blob:; frame-src 'self' http: https: blob:;"; # 开启CSP 安全策略
#server_tokens off; # 隐藏nginx 版本号
location / {
root /home/web/dist; #站点静态文件所在目录
index index.html index.htm; #入口文件
#前端history路由支持
try_files $uri $uri/ /index.html;
}
}
2.https配置
server {
listen 80;
server_name example.com;
rewrite ^(.*) https://example.com permanent;
}
server {
listen 443;
server_name example.com;
ssl on;
ssl_certificate /etc/nginx/cert/example.com.pem; #证书pem所在服务器地址
ssl_certificate_key /etc/nginx/cert/example.com.key; #证书key所在服务器地址
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /home/web/dist;
index index.html index.htm;
#前端history路由支持
try_files $uri $uri/ /index.html?$query_string;
if (!-e $request_filename){
rewrite ^/(.*) /index.html last;
}
try_files $uri $uri/ /index.html =404;
}
}
3.同一个域名http与https两种方式都可访问
#核心就是把ssl on;这行注释掉, ssl写在443端口后面, 这样http和https的链接都可以用
server {
listen 80;
listen 443 ssl; # ssl写在443端口后面
server_name example.com;
# ssl on; # 这行注释掉
ssl_certificate /etc/nginx/cert/1_example.com_bundle.crt;
ssl_certificate_key /etc/nginx/cert/2_example.com.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /usr/app/h5;
index index.html index.htm;
#前端history路由支持
try_files $uri $uri/ /index.html?$query_string;
if (!-e $request_filename){
rewrite ^/(.*) /index.html last;
}
}
}
4.多项目配置
location / {
root /usr/wcl;
try_files $uri /index.html;
}
location /zibo/ {
alias /usr/wcl/zibo/;
}
location /qingdao/ {
alias /usr/wcl/qingdao/;
}
5.gzip
# 开启gzip on为开启,off为关闭
gzip on;
# 检查是否存在请求静态文件的gz结尾的文件,如果有则直接返回该gz文件内容,不存在则先压缩再返回
gzip_static on;
# 设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。
# 默认值是0,不管页面多大都压缩。
# 建议设置成大于10k的字节数,配合compression-webpack-plugin
gzip_min_length 10k;
# 对特定的MIME类型生效,其中'text/html’被系统强制启用
gzip_types text/javascript application/javascript text/css application/json;
# Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果
# 匹配的前提是后端服务器必须要返回包含"Via"的 header头
# off(关闭所有代理结果的数据的压缩)
# expired(启用压缩,如果header头中包括"Expires"头信息)
# no-cache(启用压缩,header头中包含"Cache-Control:no-cache")
# no-store(启用压缩,header头中包含"Cache-Control:no-store")
# private(启用压缩,header头中包含"Cache-Control:private")
# no_last_modefied(启用压缩,header头中不包含"Last-Modified")
# no_etag(启用压缩,如果header头中不包含"Etag"头信息)
# auth(启用压缩,如果header头中包含"Authorization"头信息)
# any - 无条件启用压缩
gzip_proxied any;
# 请求加个 vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩
gzip_vary on;
# 同 compression-webpack-plugin 插件一样,gzip压缩比(1~9),
# 越小压缩效果越差,但是越大处理越慢,一般取中间值
gzip_comp_level 6;
# 获取多少内存用于缓存压缩结果,‘16 8k’表示以8k*16 为单位获得。
# PS: 如果没有.gz文件,是需要Nginx实时压缩的
gzip_buffers 16 8k;
# 注:99.99%的浏览器基本上都支持gzip解压了,所以可以不用设这个值,保持系统默认即可。
gzip_http_version 1.1;