Nginx 配置记录
2024年8月17日...大约 1 分钟
防盗链
#防以 jpg或png 结尾的文件盗链 (~)使用正则匹配,忽略大小写
location ~ .(jpg|png)$
{
valid_referers none blocked www.example.com *.example.com;
root html;
if ($invalid_referer){ return 403; }
}
方法2
location ~ .(jpg|png)$
{
if ($host != ".example.com") {return 403;}
}
rewrite 重写规则
格式: rewrite regex replacment [flag]
#所有http的请求,都转到 https 上去
server {
listen 80;
server_name localhost;
rewrite ^/(.*)$ https://www.example.com/$1 permanent;
…………
…………
return 301 https://$server_name$request_uri; ;
这是nginx最新支持的写法,这个写法需要注意的是:
server_name 要修改成相应的域名或 IP 地址,否则不能访问
使用自己生成的 ssl 证书配置 nginx 的 https
生成服务端的 rsa 证书及密钥。
openssl genrsa -des3 -out server.key 1024
创建请求签名
openssl req -new -key server.key -out server.csr
加载 ssl 支持的 nginx ,并使用私钥时,去除口令。
cp server.key server.key.bak
opensll rsa -in server.key.bak -out server.key
自动签发证书 (3650 大概10年)
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

配置 nginx ,nginx 在编译时需要 加入 –with-http_ssl_module 参数
复制证书及密钥到conf 目录下(配置文件所在目录,如果不复制,配置 nignx 时可以指定绝对路径)
server {
listen 443 ssl;
server_name www.example.com;
#我的配置文件放在 conf/certs 目录中
ssl_certificate certs/server.crt;
ssl_certificate_key certs/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
root html;
index index.html index.htm index.php;
}
}
贡献者
changelichangyangccm@163.com