nginx-常用配置
入门配置
#user nobody; //用户权限
worker_processes 1; //进程数
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
//事件
events {
worker_connections 1024; //最大连接数
}
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;
sendfile on; //属性值为on表示启用sendfile,sendfile的作用是在底层拷贝数据的时候可以跳过应用,直接从内核拷贝到网卡,加快速度
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;//保持时间
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
//正则表达式情况
#location ~ /\.ht {
# deny all;
#}
}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
静态页面配置
server {
listen 8080;
server_name open.unionpay.com;
location /apitest {
proxy http
}
}
动静分离
server {
listen 8080;
server_name open.unionpay.com;
location /apitest {
proxy http
}
location /static {
alias https://ip:port/static/
}
//正则表达式
location ~*\.(css|js|gif) {
root https://ip:port/static/
}
//具体匹配
location = /baidu.com {
proxy_pass https://www.baidu.com
}
}
图片防盗配置
//正则表达式
location ~*\.(css|js|gif) {
root https://ip:port/static/
//只允许*.unionpay.com访问此路径
//none blocked只能在页面中加载图片,无法直接获取
valid_referers none blocked *.unionpay.com
if ($invalid_referers) {
return 403;
}
}
黑白名单配置
deny ip //黑名单,单独用于server内
//文件用法,创建文件balack.ip,内容为
//deny ip1
//deny ip2
//引入文件,可全局引入也可局部server引入
include black.ip
//白名单文件,white.ip
ip1 1;
ip2 1;
ip3 1;
//引入
geo $remote_addr $ip_whitelist {
default 0;
include white.ip;
}
server {
listen 80;
server_name localhost;
location / {
if ($ip_whitelist = 0) {
return 403;
}
root html;
index index.html index.htm;
}
}
下载限速配置
//速度大于5k则限速到1k
limit_rate 1k;
limit_rate_after 5k;
日志配置
//日志格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
$remote_addr, $http_x_forwarded_for 记录客户端IP地址
$remote_user记录客户端用户名称
$request记录请求的URL和HTTP协议(GET,POST,DEL,等)
$status记录请求状态
$body_bytes_sent发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。
$bytes_sent发送给客户端的总字节数。
$connection连接的序列号。
$connection_requests 当前通过一个连接获得的请求数量。
$msec 日志写入时间。单位为秒,精度是毫秒。
$pipe如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。
$http_referer 记录从哪个页面链接访问过来的
$http_user_agent记录客户端浏览器相关信息
$request_length请求的长度(包括请求行,请求头和请求正文)。
$request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
$time_iso8601 ISO8601标准格式下的本地时间。
$time_local通用日志格式下的本地时间。
配置的时候,文件还可以根据网址命名,如:
access_log logs/$host.access.log main;
如果原来没有这个文件,那就需要创建,创建需要权限,要在整体配置文件的第一行加上
user root;

浙公网安备 33010602011771号