主站配置
nginx 配置文件目录一般在 /usr/local/nginx/conf/nginx.conf 下,当然我们也可以自己指定,参见文末。
配置好 root 目录,nginx 会寻找目录下的 index index.html index.htm index.php 作为网站的入口,因为我主站是 WordPress 搭建的,所以会有 index.php

下面的各个 location 是默认配置,主要填好 server_name 和 root 路径对应的即可
server {
server_name nanoi.site;
index index.html index.htm index.php;
root /home/wwwroot/wordpress;
error_page 404 /404.html;
include enable-php.conf;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /nginx_status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\. {
deny all;
}
}
SSL 配置
个人用户建议使用 certbot,以下一行代码即可完成部署的站点的自动证书申请配置与续期
certbot --nginx --nginx-server-root /usr/local/nginx/conf
certbot 在完成证书签发后会自动在我们的 nginx 配置文件中加上 ssl 配置。
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/nanoi.site/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/nanoi.site/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
多站点配置
我们可能会有使用一台服务器搭建多个站点的需求(比如我),只要申请多个子域名并在 nginx config 中配置多个 server 块即可实现。
例如,我的另一个剪切板站点是 Docker 在 8022 端口启动的,那么只需要加入如下配置
server_name对应子域名proxy_pass就是所谓的反向代理,将请求代理到对应端口
server {
server_name p.nanoi.site;
location / {
proxy_pass http://127.0.0.1:8022;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/nanoi.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/nanoi.site/privkey.pem; # managed
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
子域名配置
直接在自己的域名提供商添加对应的前缀 A 记录即可


常用命令
- 修改好配置之后测试语法,
nginx -t - 修改好配置之后使得配置生效,需要重启
nginx,nginx -s reload - 以指定的配置文件启动,
nginx -c /path/to/your-config.conf