静态代理配置
server {
listen 80;
server_name xxxxx.com; #修改域名
location / {
root /usr/share/nginx/html/docs; #代理到docs文件夹中
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
动态代理配置
- 动态代理就是把代理服务器的请求转发到另一个服务上去
server {
listen 80;
server_name xxxxx.com; #修改域名
location / {
proxy_pass http://xxx.xx.xx.x:8080; #修改为代理服务地址
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
文件压缩—开启gzip
- 如果我们租用了一个带宽很低的服务器,网站访问速度会很慢,这时我们可以通过让nginx开启GZIP压缩来提高网站的访问速度。这里我们以mall的前端项目为例来演示下它的提速效果。
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on; #开启gzip
gzip_disable "msie6"; #IE6不使用gzip
gzip_vary on; #设置为on会在Header里增加 "Vary: Accept-Encoding"
gzip_proxied any; #代理结果数据的压缩
gzip_comp_level 6; #gzip压缩比(1~9),越小压缩效果越差,但是越大处理越慢,所以一般取中间值
gzip_buffers 16 8k; #获取多少内存用于缓存压缩结果
gzip_http_version 1.1; #识别http协议的版本
gzip_min_length 1k; #设置允许压缩的页面最小字节数,超过1k的文件会被压缩
gzip_types application/javascript text/css; #对特定的MIME类型生效,js和css文件会被压缩
#include D:\\java\\nginx-1.17.1\\conf\\nginx.conf;
server {
listen 8082;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html ;
index index.html index.htm;
}
}
}
访问页面查看 response header中有Content-encoding:gzip表示开启成功
地址重写
有的时候我们的网站更换了域名,但还有用户在使用老的域名访问,这时可以通过nginx的地址重写来让用户跳转到新的域名进行访问。
-
比如说原来我们用的slim.com这个域名不用了,现在改成www.slimshady.com了来访问文档项目了;
-
修改conf配置文件,将地址带参数重写到新地址:
server {
listen 80;
server_name slim.com;
rewrite "^/(.*)$" http://www.slimshady.com/$1; #地址重写到新地址
location / {
root /usr/share/nginx/html/docs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
此时访问旧域名slim.com会直接跳转到www.slimshadhy.com去。