博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx 高级配置示例.
阅读量:6869 次
发布时间:2019-06-26

本文共 4048 字,大约阅读时间需要 13 分钟。

一、用户认证

用户认证功能是利用Apache的工具htpasswd生成的密钥,所以需要安装Apache的这个工具即可,我们用yum来安装就可以。

[root@localhost ~]# yum install -y httpd

[root@localhost ~]# htpasswd -c /usr/local/nginx/conf/.htpasswd test

New password:

Re-type new password:    ##创建第二个账户密码,不需要-c

Adding password for user mydiscuz

[root@localhost ~]# cat /usr/local/nginx/conf/.htpasswd

mydiscuz:$apr1$ejPLa15T$kuyykf8at2I77oogZ0kUz1

    修改配置,主要是修改server模块

location ~ .*admin\.php$ {

        auth_basic "testlinux auth";

        auth_basic_user_file /usr/local/nginx/conf/.htpasswd;

        include fastcgi_params;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

        }

[root@localhost ~]#/usr/local/nginx/sbin/nginx -t   检查

[root@localhost ~]# service nginx reload   重新加载

二、域名重定向(域名跳转)

cd /usr/local/nginx/conf/vhosts/--> vim test.conf插入

server

{

    listen 80;

    server_name www.test.com www.aaa.com www.bbb.com;

    if ($host   !=   'www.test.com')

    {

        rewrite   ^/(.*)$   http://www.test.com/$1   permanent;

     }

    index index.html index.htm index.php;

    root /data/www;

    location ~ .*admin\.php$ {

以下无改动

2. /usr/local/nginx/sbin/nginx -t  检查。 /usr/local/nginx/sbin/nginx -s reload 重新加载

3.在C:\Windows\System32\drivers\etc找到hosts用文本打开在

# localhost name resolution is handled within DNS itself.

#        127.0.0.1       localhost

#        ::1             localhost

192.168.52.120  www.test.com  www.aaa.com  www.222.com  

三、静态文件缓存与不记录指定文件日志

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

     expires      30d;

     access_log off;

     }

location ~ .*\.(js|css)?$

    {

     expires      12h;

     access_log off;

     }

四、防盗链

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)$       

#针对这些文件进行防盗链配置

    {

        access_log off;

        expires 15d;

        valid_referers none blocked  *.test.com *.aaa.com *.bbb.com;  #只允许这几个域名

        if ($invalid_referer)

        {

             return 403;

#       rewrite ^/ http://www.example.com/nophoto.gif;   ##可以跳转到某图片

        }

    }

                       

               

/usr/local/nginx/sbin/nginx  -t  检验   /usr/local/nginx/sbin/nginx -s  reload  重启

五、设置日志记录的内容

1. vim /usr/local/nginx/conf/nginx.conf  做出如下更改

http

{

    include mime.types;

    default_type application/octet-stream;

    server_names_hash_bucket_size 3526;

    server_names_hash_max_size 4096;

    log_format test  '$remote_addr $http_x_forwarded_for [$time_local]'

    '$host "$request_uri" $status'

    '"$http_referer" "$http_user_agent"';

    sendfile on;

2.vim test.conf配置文件

    index index.html index.htm index.php;

    root /data/www;

    access_log /tmp/access.log test;   自定义地址及名字最好是比较大的一个磁盘下面

六 、日志切割

因为Nginx没有自动切割日志功能,所以需要手动编辑脚本

1.脚本实现

#vim  /usr/local/sbin/nginx_log.sh

nginx日志按日期自动切割脚本如下:

#!/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 reload > /dev/null                                    

cd /tmp/nginx_log/

gzip -f $d.log                             #压缩切割后的额脚本

2.定时工作

在crontab中设置作业

#crontab -e

0 0 * * * bash /usr/local/sbin/nginx_log.sh       

七、访问控制

   1. 限制只让某个ip访问 

    allow          219.232.244.234;

    deny           all;

2.禁止某个IP或者IP段访问站点的设置方法

首先建立下面的配置文件放在nginx的conf目录下面,命名为deny.ip   

cat  deny.ip

deny 192.168.1.11;

deny 192.168.1.123;

deny 10.0.1.0/24;

在nginx的配置文件nginx.conf中加入:

include deny.ip; 

重启一下nginx的服务:/usr/local/nginx/sbin/nginx  reload 就可以生效了。 

deny.ip 的格式中也可以用deny all; 

如果你想实现这样的应用,除了几个IP外,其他全部拒绝,

那需要你在deny.ip 中这样写

allow 1.1.1.1; 

allow 1.1.1.2;

deny all;

3.有时候会根据目录来限制php解析:

location ~ .*(diy|template|attachments|forumdata|attachment|image|admin)/.*\.php$ 

{

      allow 127.0.0.1;   

         deny all;

}

八、使用 user_agent 控制客户端访问 

location / 

{

    if ($http_user_agent ~ 'bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315'){

            return 403;

    }

}

九、Nginx代理

1.绑定多个地址:yum install bind*先安装-->dig www.baidu.com  查询到多个地址

#cd    /usr/local/nginx/conf/vhosts  

#vim  proxy.conf  编辑如下:

upstream test{

    server  61.135.169.125:80;

    server  61.135.169.121:80;

}

server {

    listen 80;

    server_name www.baidu.com;

    location / {

        proxy_pass http://test/;

        proxy_set_header Host $host;

    }

}

2.绑定一个地址:可通过ping  www.baidu.com 获得。如果无需vim  /etc/hosts删除百度相关一行,重新ping

server { 

    listen 80;

    server_name www.baidu.com;

    location / {

        proxy_pass http://61.135.169.121/;

        #proxy_set_header Host $host;

    }

}

3.用 curl -x127.0.0.1:80 www.baidu.com 测试正常,代理访问完成

本文转自super李导51CTO博客,原文链接: http://blog.51cto.com/superleedo/1890336,如需转载请自行联系原作者
你可能感兴趣的文章
Android 之 用WebView显示网页
查看>>
go——搭建Win7下的Go开发环境
查看>>
ubuntu14.04 中国源
查看>>
学一学书里的django是怎么写views.py的
查看>>
微信支付开发(8) 刷卡支付
查看>>
scriptcs简介
查看>>
ajax-原理分析
查看>>
【leetcode】Jump Game I, II 跳跃游戏一和二
查看>>
【ML入门系列】(三)监督学习和无监督学习
查看>>
springboot 配置jsp支持
查看>>
window.open实现模式窗口
查看>>
椭圆曲线密码体制(ECC)简介
查看>>
Mac OS 终端利器 iTerm2
查看>>
使用贝塞尔曲线进行插值 一种非常简单的平滑多边形的方法
查看>>
WebMvcConfigurerAdapter已经过时的问题解决
查看>>
C# activex开发中 axwebbrowser控件及 IE浏览器设置
查看>>
032 报错
查看>>
喵喵的技术学习之路(一)
查看>>
C#(winform)设置窗体的启动位置
查看>>
【iOS开发-48】九宫格布局案例:自己主动布局、字典转模型运用、id和instancetype差别、xib反复视图运用及与nib关系...
查看>>