4、SERVER 模块
server {
#监听端口,nginx 会根据请求的 HOST 来决定使用哪个 SERVER 段的配置。如果没有匹配的 server_name,则默认使用配置文件中第一个。加上 default_server 则可以以指定没有匹配时的默认规则。
#listen 80;
listen 80 default_server;
#域名可以有多个,用空格隔开
server_name www.test.com test.com;
root /user/share/nginx/html/test;
#404页面配置
error_page 404 /404.html;
#配置 ssl,有需要时开启。
ssl on;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
location / {
index index.html index.php;
}
#图片缓存时间设置
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 10d;
}
#JS和CSS缓存时间设置
location ~ .*.(js|css)?$ {
expires 1h;
}
location ~ [^/].php(/|$) {
fastcgi_index index.php;
#开启 PATH_INFO 支持,作用就是把参数按照给定的正则表达式分割成 $fastcgi_script_name 和 $fastcgi_path_info。
#例如:请求 index.php/id/1 不加此行配置时,fastcgi_script_name 是 /index.php/id/1,fastcgi_path_info 是空。
#加上之后,fastcgi_script_name 是 index.php,fastcgi_path_info 是 /id/1
fastcgi_split_path_info ^(.+.php)(.*)$;
#此值即是 PHP 中 $_SERVER['SCRIPT_FILENAME'] 的值
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
#指定FastCGI服务器监听端口与地址。须和 PHP-FPM 的设置相同。
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
二、常见的方式
让木马上传后不能执行:针对上传目录,在nginx配置文件中加入配置,使此目录无法解析php 让木马执行后看不到非网站目录文件:取消php-fpm运行账户对于其他目录的读取权限 木马执行后命令不能执行:取消php-fpm账户对于sh的执行权限 命令执行后权限不能过高:php-fpm账户不要用root或者加入root组三、具体的配置
1、禁止php文件的访问及执行
location ~ /(attachments|upload)/.*.(php|php5)?$ {
deny all;
}
2、禁止IP的访问
//禁止的写法 deny 10.0.0.0/24; //允许的写法 allow 10.0.0.0/24; deny all;
3、根据用户的真实 IP 做连接限制
## 这里取得原始用户的IP地址
map $http_x_forwarded_for $clientRealIp {
"" $remote_addr;
~^(?P<firstAddr>[0-9.]+),?.*$ $firstAddr;
}
## 针对原始用户 IP 地址做限制
limit_conn_zone $clientRealIp zone=TotalConnLimitZone:20m ;
limit_conn TotalConnLimitZone 50;
limit_conn_log_level notice;
## 针对原始用户 IP 地址做限制
limit_req_zone $clientRealIp zone=ConnLimitZone:20m rate=10r/s;
#limit_req zone=ConnLimitZone burst=10 nodelay;
limit_req_log_level notice;
## 具体服务器配置
server {
listen 80;
location ~ .php$ {
## 最多 5 个排队, 由于每秒处理 10 个请求 + 5个排队,你一秒最多发送 15 个请求过来,再多就直接返回 503 错误给你了
limit_req zone=ConnLimitZone burst=5 nodelay;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}








