Nginx基础代理
server {
listen 8000; // 监听端口
server_name test.com; // 监听地址
location / { // 代理配置
proxy_pass http://127.0.0.1:8888 // 代理地址
proxy_set_header Host $host
// 设置代理请求头(host在到达目的主机时默认为代理地址,该设置可以将其更改为监听地址)
}
}
Nginx代理配置
配置缓存
proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m; // 配置缓存路径
location / {
proxy_cache my_cache; // 配置缓存
}
如果服务端有返回Cache-Control
头,且值包含max-age
和s-max-age
,则前者将用于浏览器缓存,后者用于代理服务器缓存。
如果请求头有修改,则不会使用缓存。
HTTPS解析
使用Nginx部署HTTPS
使用指令生成证书
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout localhost-privkey.pem -out localhost-cert.pem
配置https
server {
listen 443; // https默认端口为443
server_name test.com;
ssl on; // 打开ssl
ssl_certificate_key ../certs/localhost-privkey.pem; // 私钥地址
ssl_certificate ../certs/localhost-cert.pem; // 公钥地址
}
代理http到https
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name test.com;
return 302 https://$server_name$request_uri;
}
HTTP2
http2的优势
- 信道复用
- 分帧传输
- Server Push
Nginx开启http2
server {
listen 443 http2; // http2现在还只能在https的基础上开启
http2_push_preload on; // 开启服务端推送
}