一、Nginx
配置
user root;
worker_processes 1;
events {
worker_connections 1024;
}
1. http
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
}
2. server
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3. location
location ~ \.php$ {
proxy_pass http://127.0.0.1;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
3.1 root
站点根目录
location /qs {
root /www/qs/
index index.html index.htm;
}
3.2 alias
站点别名
location /qs {
alias /www/qs;
index index.html index.htm;
}
4. upstream
属性 | 描述 |
---|
service | 反向服务地址: 加端口 |
weight | 权重 |
max_fails | 失败多少次 认为主机已挂掉则,踢出 |
fail_timeout | 踢出后重新探测时间 |
backup | 备用服务 |
max_conns | 允许最大连接数 |
slow_start | 当节点恢复,不立即加入 |
4.1 负载均衡算法
least_conn
最少链接least_time
最小的响应时间。
5. 日志配置
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;
5.1 基于域名打印日志
access_log logs/$host.access.log main;
5.2 error
日志
5.3 指定客户端输出debug
级别日志
events {
debug_connection 192.168.0.147;
debug_connection 10.224.57.0/200;
}
二、Nginx
案例
1. 动静分离
1.1 基于目录动静分离
server {
listen 80;
server_name *.qs.com;
root /usr/www/qs;
location / {
index qs.html;
}
location /static {
alias /usr/www/static;
}
}
1.2 基于正则动静分离
location ~* \.(gif|jpg|png|css|js)$ {
root /usr/www/static;
}
2. 防盗链
location ~* \.(gif|png|jpg|swf|flv)$ {
root html;
valid_referers none *.qs.com;
if ($invalid_referer) {
rewrite ^/ http://www.qs.com/image/403.png;
}
}
valid_referers none blocked *.qs.com;
if ($invalid_referer) {
return 403;
}
3. 下载限速
location /download {
limit_rate 1m;
limit_rate_after 30m;
}
5. IP
黑名单
location ~ /\.ht {
deny all;
}
deny 192.168.0.1;
allow 192.168.0.2;
deny 192.168.0.0/24;
allow 192.168.0.25/30;
deny all;
allow all;
include black.ip;
echo 'deny 192.168.0.132;' >> balck.ip
6. 子域名站点
server {
listen 80;
server_name *.qs.com;
root /data/www/$host;
access_log logs/$host.access.log;
location / {
index index.html;
}
}
7. nginx_status
location /nginx_status {
stub_status on;
access_log off;
}
8. stub_status
location = /basic_status {
stub_status;
}