nginx配置

最近折腾的web应用比较多,在nginx使用中踩了不少坑,记录一下,以便以后查询

安装

1
2
apt update
apt install -y nginx

配置文件路径

/etc/nginx/conf.d

建议每个站点单独建一个 ***.conf 放在此目录

ssl证书位置

默认相对目录为 /etc/nginx ,建议新建一个目录来存放,如 /etc/nginx/cert,
则在配置中填写为: cert/证书名

亦可将证书放在其他地方,配置文件中使用绝对路径

http配置

1
2
3
4
5
6
server {
listen 80; # 站点监听端口
server_name zfile.singlelovely.cn ; # 站点域名
root /www/test; # 网站根目录
index index.html index.htm index.jsp;
}

https配置

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 443;
server_name zfile.singlelovely.cn;
ssl on;
root /www/test;
index index.html index.htm index.jsp
ssl_certificate cert/1_zfile.singlelovely.cn_bundle.crt;
ssl_certificate_key cert/2_zfile.singlelovely.cn.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
}

反向代理配置

1
2
3
4
5
6
7
8
9
10
11
12
location / {
proxy_pass http://127.0.0.1:666;
index index.html index.htm;
proxy_redirect off;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

http 强制重定向到https

rewrite ^ https://$http_host$request_uri? permanent;

检查配置问题

nginx -t

重启服务

service nginx restart

0%