HTTPS Nginx配置HTTPS
server{
listen 443 ssl;
server_name www.nextdevops.cn;
ssl_certificate ../crt/www.nextdevops.cn.crt;
ssl_certificate_key ../crt/www.nextdevops.cn.key;
location / {
index index.html;
root /opt/web;
}
}
referer防盗链
load_module modules/ngx_stream_module.so; #动态加载模块,必须写道开头
user nginx; #使用useradd nginx 添加一个nginx用户
worker_processes 4; #cpu核心数 * 2
worker_rlimit_nofile 102400; #配置nginx打开最大文件数 (每个工作进程绑定一个cpu,worker_cpu_affinity配置)
worker_cpu_affinity 0001 0010 0100 1000; #工作进程使用哪个cpu的核心 (以四核为例) 0001是4核的第一个核心 0010是4核的第二个核心
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main; #在server虚拟目录里面配置日志,这里是全局日志
sendfile on;
#tcp_nopush on;
server_tokens off; #错误的时候关闭输出版本号
#keepalive_timeout 0;
keepalive_timeout 30;
gzip on; #压缩会占用cpu
gzip_buffers 4 16k;
gzip_comp_level 3; #压缩等级
gzip_disable "MSIE[1-6]"; #ie浏览器1-6禁用gzip
gzip_min_length 1k;
gzip_http_version 1.0;
gzip_types text/plaion application/html application/css application/js; #可以压缩的文件类型
gzip_vary on; #根据http头判断是否支持压缩
client_max_body_size 8m; #默认允许客户端最大上传文件大小
server{
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#可用的referer才能访问资源
#none 没有这个字段
#blocked 有referer,但是字段会被防火墙删除,没有值的情况下
#server_names 可信任的本站的server_name
location ~ \.(jpg|png|mp4){
valid_referers none blocked server_names *.nextdevops.cn nextdevops.* www.example.org/inages/ ~\.google\. ~\.baidu\.;
#上面匹配有效, $invalid_referer变量的值就会1,为真
if($invalid_referer){
return 403;
}
}
}
}
secure_link模块防盗链
load_module modules/ngx_stream_module.so; #动态加载模块,必须写道开头
user nginx; #使用useradd nginx 添加一个nginx用户
worker_processes 4; #cpu核心数 * 2
worker_rlimit_nofile 102400; #配置nginx打开最大文件数 (每个工作进程绑定一个cpu,worker_cpu_affinity配置)
worker_cpu_affinity 0001 0010 0100 1000; #工作进程使用哪个cpu的核心 (以四核为例) 0001是4核的第一个核心 0010是4核的第二个核心
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main; #在server虚拟目录里面配置日志,这里是全局日志
sendfile on;
#tcp_nopush on;
server_tokens off; #错误的时候关闭输出版本号
#keepalive_timeout 0;
keepalive_timeout 30;
gzip on; #压缩会占用cpu
gzip_buffers 4 16k;
gzip_comp_level 3; #压缩等级
gzip_disable "MSIE[1-6]"; #ie浏览器1-6禁用gzip
gzip_min_length 1k;
gzip_http_version 1.0;
gzip_types text/plaion application/html application/css application/js; #可以压缩的文件类型
gzip_vary on; #根据http头判断是否支持压缩
client_max_body_size 8m; #默认允许客户端最大上传文件大小
server{
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#secure_link和secure_link_md5指令启用
location /download/ {
secure_link $arg_md5,$arg_expires;
secure_link_md5 "$secure_link_expires$uri$remote_addr secret";
if($secure_link = ""){
return 403;
}
if($secure_link = "0"){
return 410;
}
}
#secure_link_secret指令启用,检查请求连接的真实性
location /p/ {
secure_link_secret secret;
if($secure_link = ""){
reutrn 403;
}
rewrite ^ /secure/$secure_link;
}
location /secure/{
internal;
}
}
}
访问控制
IP白名单
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
#屏蔽单个IP的命令是
deny 192.168.1.1
#封整个段即从123.0.0.1到123.255.255.254的命令
deny 123.0.0.0/8
#封IP段即从123.45.0.1到123.45.255.254的命令
deny 124.45.0.0/16
#封IP段即从123.45.6.1到123.45.6.254的命令是
deny 123.45.6.0/24
也就是说,如果最后的斜杠后的数值:
8:匹配后三位最大值的
16:匹配后两位最大值的
24:匹配后一位最大值的
#http身份认证
load_module modules/ngx_stream_module.so; #动态加载模块,必须写道开头
user nginx; #使用useradd nginx 添加一个nginx用户
worker_processes 4; #cpu核心数 * 2
worker_rlimit_nofile 102400; #配置nginx打开最大文件数 (每个工作进程绑定一个cpu,worker_cpu_affinity配置)
worker_cpu_affinity 0001 0010 0100 1000; #工作进程使用哪个cpu的核心 (以四核为例) 0001是4核的第一个核心 0010是4核的第二个核心
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main; #在server虚拟目录里面配置日志,这里是全局日志
sendfile on;
#tcp_nopush on;
server_tokens off; #错误的时候关闭输出版本号
#keepalive_timeout 0;
keepalive_timeout 30;
gzip on; #压缩会占用cpu
gzip_buffers 4 16k;
gzip_comp_level 3; #压缩等级
gzip_disable "MSIE[1-6]"; #ie浏览器1-6禁用gzip
gzip_min_length 1k;
gzip_http_version 1.0;
gzip_types text/plaion application/html application/css application/js; #可以压缩的文件类型
gzip_vary on; #根据http头判断是否支持压缩
client_max_body_size 8m; #默认允许客户端最大上传文件大小
#需要生成一个../conf/passwd.db
#htpasswd -c -m passwd.db user01
#设置密码123456 ,确认123456
server{
listen 80;
server_name localhost;
index index.html;
root /html/www;
location / {
auth_basic "Please enter user name and password";
auth_basic_user_file ../conf/passwd.db;
}
}
#限流end
}