1. 基础的nginx 配置
server {
linsten : 80
server_name test.com
location/ {
proxy_pass http://127.0.0.1:8888
proxy_set_header HOST @host
}
}
2. 配置缓存
levels 配置cache保存时是否创建二级目录。
keys_zone 内存中保存缓存的空间大小
my_cache 是使用缓存的名称
cache 是缓存文件大小
proxy_catch_path cache levels=1:2 keys_zone=my_cache:10m
server {
linsten : 80
server_name test.com
location/ {
proxy_cache my_cache
proxy_pass http://127.0.0.1:8888
proxy_set_header HOST @host
}
}
3. nginx 配置HTTPs (HTTPS = http + security)
先确认 https 公钥私钥的在本地路径位置。比如 公钥 ../path_public; 私钥../path-private
proxy_catch_path cache levels=1:2 keys_zone=my_cache:10m
server {
linsten : 443;
server_name test.com;
ssl on;
ssl_certificate_key ../path-private;
ssl_certificate ../path_public;
location/ {
proxy_cache my_cache;
proxy_pass http://127.0.0.1:8888;
proxy_set_header HOST @host;
}
}
4. http 访问自动跳到 https
proxy_catch_path cache levels=1:2 keys_zone=my_cache:10m
server{
linsten 80 default_server;
linsten [::]:80 default_server;
server_name test.com
return 302 http://$server_name@request_uri;
}
server {
linsten : 443;
server_name test.com;
ssl on;
ssl_certificate_key ../path-private;
ssl_certificate ../path_public;
location/ {
proxy_cache my_cache;
proxy_pass http://127.0.0.1:8888;
proxy_set_header HOST @host;
}
}
5. nginx 配置 http2(分帧传输,信道复用) (只支持https)
proxy_catch_path cache levels=1:2 keys_zone=my_cache:10m
server{
linsten 80 default_server;
linsten [::]:80 default_server;
server_name test.com
return 302 http://$server_name@request_uri;
}
server {
linsten : 443 http2;
server_name test.com;
http2_push_preload on; 开启preload
ssl on;
ssl_certificate_key ../path-private;
ssl_certificate ../path_public;
location/ {
proxy_cache my_cache;
proxy_pass http://127.0.0.1:8888;
proxy_set_header HOST @host;
}
}