LNMP之nginx的基本配置

来源:互联网 发布:游族网络 刀剑乱舞 编辑:程序博客网 时间:2024/05/21 19:44
注:nginx 的nginx.conf文件中 listen监控的端口要和 php-fpm listen的监控端口一致,这样就没有502的问题。
1.Nginx用户认证
location ~ .*admin\.php {
auth_basic "eric auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
2.域名跳转301/302重定向
server_name www.test.com www.aaa.comwww.bbb.com
if (host !='www.test.com'){
rewrite ^/(.*)$ http://www.test.com/$1 permanet;
}

3.不记录指定了类型文件日记
location ~ .*\.(gif|jpg|jpeg|swf)
{
access_log off;
}
location ~ (static|cache)
{
access_log off;
}
4.日记切割
vim /usr/local/sbin/nginx_logrotate.sh
----------------
#!/bin/bash
d= `date -d "-1 day" +%F`
[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log/
mv /tmp/access.log /tmp/nginx_log/$d.log
/etc/init.d/nginx restart > /dev/null
cd /tmp/nginx_log/
gzip -f $d.log
执行脚本
sh -x /usr/local/sbin/nginx_logrotate.sh
5.设置文件失效时间
location ~ .*\.(gif|jpg|jpeg|rar|png)$
{
access_log off;
expires 2h;
}
6.防盗链配置
location ~ .*\.(gif|jpg|jpeg|rar|png|rar|zip|flv|mp3)$
{
access_log off;
expires 2h;
valid_referers none blocked *.test.com *.aaa.com *.aminglinux.com;
if ($invalid_referer)
{
return 403;
}
}
7.nginx 访问控制
黑名单
deny 127.0.0.1
白名单
allow 192.168.141.133
deny all
某个文件夹下实现访问控制
location ~ .*\.admin.php
{
allow 192.168.141.133
deny all
auth_basic "eric auth"
auth_basic_user_file /usr/local/nginx/conf/.htpasswd
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
8.禁止指定user_agent
if ($http_user_agent ~ 'curl|baidu|1111')
{
return 403;
}
9.Nginx反向代理
譬如代理www.baidu.com
首先ping出百度的ip
其次在nginx虚拟主机文件夹下编辑配置文件
vim /usr/local/nginx/conf/vhost/proxy.conf
server
{
listen 80;
server_name www.baidu.com;
location /{
proxy_pass http://'插入百度IP/';
proxy_set_header Host $host;
}
}
负载均衡写法:
upstream eric{
server xxxxxx;
server xxxxx ;
}
server
{
listen 80;
server_name www.baidu.com;
location /{
proxy_pass http://eric/';
proxy_set_header Host $host;
}
}







1 0
原创粉丝点击