nginx站点配置
使用 docker 容器,使用 docker-compose 管理
compose 目录 /root/nginx-container
nginx 配置文件:
1/root/nginx-container/conf/nginx.conf
2/root/nginx-container/conf.d/*.conf
SSL 证书存放路径,在此目录再以项目或站点名称创建文件夹存放
1/root/nginx-container/ssl/
目前 sxsc 站点的配置文件 /root/nginx-container/conf/conf.d/sxsc.ltd.conf ,
SSL 证书存放路径 /root/nginx-container/ssl/sxsc.ltd/ .
添加新站点
在 /root/nginx-container/conf/conf.d/ 目录下创建一个新文件,并以 .conf 结尾,
可以用站点名称命名,如:www.sxsc.com.conf, 配置文件内容,可参考以下
1server {
2 listen 80; # 监听端口
3 server_name sxsc.ltd www.sxsc.ltd; # 域名、主机名
4 # 以下是 http 跳转到 https,并返回 HTTP 301 永久重写向.
5 # 适合开启了https的站点,若用户从https访问则自动跳转到https
6 location / {
7 rewrite ^/(.*)$ https://www.sxsc.ltd/$1 permanent;
8 }
9}
10server {
11 listen 443 ssl; # 监听端口,并开启 SSL
12 server_name sxsc.ltd www.sxsc.ltd; # 域名、主机名
13 root /data/www/html/sxsc; # 网站根目录
14 index index.html index.htm index.php; # index设定,即没有指定文件名时自动查找的文件
15
16 ssl_certificate ssl/sxsc.ltd/sxsc_cert.crt; # SSL 证书文件路径
17 ssl_certificate_key ssl/sxsc.ltd/sxsc_cert.key; # SSL 证书私钥文件路径
18
19 # SSL 调优
20 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # SSL 协议版本
21 ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
22 ssl_prefer_server_ciphers on;
23 ssl_session_cache shared:SSL:5m; # 开启 SSL 复用 Session,减少 TLS 握手
24 ssl_session_timeout 1h; # SSL 缓存有效期,单位:d(天)、h(时)、m(分)、
25
26 # HSTS 配置,添加 http header, 强制浏览器直接使用 https 访问,在浏览器初次打开时记录,有效期一年
27 # 再次访问,则会再次刷新。
28 # 若开启这个配置,注意 SSL 证书,不能过期。
29 # 若过期,则已经缓存这个的站点HSTS信息的浏览器,则会因为证书过期不能打开
30
31 # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
32
33 # 请求数据限制,单个IP,每秒最多32个请求,缓存区16个
34 # 一秒内,同一个IP,第49个请求,会返回 HTTP 503
35 # Request limit
36 # limit_req_zone $binary_remote_addr zone=req_zone:15m rate=32r/s;
37 # limit_req zone=req_zone burst=16;
38
39 # 禁止类似 Curl 类的工具,以及空的UA访问及抓取
40 # 若 curl 指定特别的UA,则不能限制
41 if ($http_user_agent ~* "Scrapy|HttpClient|Teleport|TeleportPro|curl|^$")
42 {
43 return 403;
44 }
45
46 # 禁止 非常规GET POST HEAD 请求
47 if ($request_method !~* ^(GET|HEAD|POST)$)
48 {
49 return 403;
50 }
51
52 # php 配置
53 location ~* [^/]\.php(/|$)
54 {
55 try_files $uri =404;
56 fastcgi_pass unix:/tmp/php-cgi.sock;
57 fastcgi_index index.php;
58 include fastcgi.conf;
59 }
60 # JS CSS文件缓存
61 location ~* .*\.(js|css)$
62 {
63 expires 2d;
64 }
65
66 # 图片缓存
67 location ~* .*\.(jpg|jpeg|tiff|tif|png|gif|bmp|ico|png|swf)$
68 {
69 expires 2d;
70 }
71
72 location ~ /\.
73 {
74 deny all;
75 }
76
77 location ~* .*\.(htaccess|log)$
78 {
79 deny all;
80 }
81}
以上为基本配置,可根据实际情况再调节
修改配置文件的后的重启
修改配置文件以后,不要直接重启,先测试文件是否有错误的地方
由于使用的是 docker ,使用 compose 管理以下是测试配置文件的命令
1cd /root/nginx-nginx-container
2docker-compose exec webngx nginx -t -c /etc/nginx/nginx.conf
3 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
4 nginx: configuration file /etc/nginx/nginx.conf test is successful
若提示 OK 则没有问题,使用命令重启或重载配置文件
1cd /root/nginx-nginx-container
2docker-compose restart # 重启
3docker-compose exec webngx nginx -s reload # 平滑重启(重载配置文件)
4docker-compose exec webngx ngins -s reopen # 重新打开文件