nginx配置用户认证、域名跳转、日志记录、静态文件缓存、防盗链

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

一、nginx配置用户认证

首先需要安装apache,可以使用yum install httpd 安装;或者在其他机器创建好.htpasswd文件,拷贝到服务器;

创建用户,并生成密码文件:

/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd  test 

// 添加test用户,第一次添加时需要加-c参数,第二次添加时不需要-c参数;


访问指定目录配置用户认证:

在nginx的default配置文件中添加,红色的部分是指定在哪个目录设置用户认证。

location  /a/ {

         auth_basic              "Auth";

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

}


实验测试,使用curl 解析/a/目录下的index.html为401未认证;使用-u 用户名密码登录之后为200 OK;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost vhosts] # curl -x127.0.0.1:80 192.168.20.30/a/index.html -I
HTTP /1 .1 401 Unauthorized
Server: nginx /1 .6.2
Date: Thu, 14 May 2015 09:48:18 GMT
Content-Type: text /html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm= "Auth"
 
[root@localhost vhosts] # curl -utest:1234 -x127.0.0.1:80 192.168.20.30/a/index.html -I
HTTP /1 .1 200 OK
Server: nginx /1 .6.2
Date: Thu, 14 May 2015 09:48:26 GMT
Content-Type: text /html
Content-Length: 612
Last-Modified: Thu, 14 May 2015 09:27:34 GMT
Connection: keep-alive
ETag:  "55546a86-264"
Accept-Ranges: bytes


访问admin.php后台,设置用户认证;设置认证的代码要写在匹配php的前面,并且也要加入解析php的代码;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost vhosts] # cat default.conf 
server
{
     listen 80 default;
     server_name localhost;
     index index.html index.htm index.php;
     root  /usr/local/nginx/html ;
     
     location ~ admin\.php {
         auth_basic               "Auth" ;
         auth_basic_user_file    /usr/local/nginx/conf/ .htpasswd;
         include fastcgi_params;
         fastcgi_pass unix: /tmp/php-fcgi .sock;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME  /usr/local/nginx/html $fastcgi_script_name;
     }
     location ~ \.php$ {
         include fastcgi_params;
         fastcgi_pass unix: /tmp/php-fcgi .sock;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME  /usr/local/nginx/html $fastcgi_script_name;
     }
}


网页测试登录admin.php页面弹出认证对话框,输入用户名密码才可以访问。

spacer.gifwKioL1VZQvnSjmMhAAHE-s7FPX0540.jpg


如不指定root目录,直接使用认证的话,打开首页弹出对话框进行认证;

    location / {

        auth_basic              "Auth";

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



二、配置域名重定向

nginx默认虚拟主机配置加入下面的代码:

    server_name 11.com 22.com www.111.com;


    if ($host != 'www.111.com' ) {
        rewrite  ^/(.*)$  http://www.111.com/$1  permanent;

    }

permanent    永久重定向301;


试验测试:访问11.com 22.com 都会跳转到location:www.111.com;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost vhosts] # curl -x127.0.0.1:80 11.com -I
HTTP /1 .1 301 Moved Permanently
Server: nginx /1 .6.2
Date: Thu, 14 May 2015 22:15:16 GMT
Content-Type: text /html
Content-Length: 184
Connection: keep-alive
Location: http: //www .111.com/
[root@localhost vhosts] # curl -x127.0.0.1:80 22.com -I
HTTP /1 .1 301 Moved Permanently
Server: nginx /1 .6.2
Date: Thu, 14 May 2015 22:15:21 GMT
Content-Type: text /html
Content-Length: 184
Connection: keep-alive
Location: http: //www .111.com/


三、配置日志记录

日志格式,main为定义的日志格式名;日志格式需加入到nginx.conf主配置文件http段中;

log_format main '$remote_addr - $remote_user [$time_local] $request '

                    '"$status" $body_bytes_sent "$http_referer" '

                    '"$http_user_agent" "$http_x_forwarded_for"';

log_format main1 '$proxy_add_x_forwarded_for - $remote_user [$time_local] '

                      '"$request" $status $body_bytes_sent '

                      '"$http_referer" "$http_user_agent"';  

//此日志格式为,ip不仅记录代理的ip还记录远程客户端真实IP。


添加访问日志的格式,写到default.conf最后一行;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server
{
     listen 80 default;
     server_name localhost;
     index index.html index.htm index.php;
     root  /usr/local/nginx/html ;
     
     location ~ \.php$ {
     include fastcgi_params;
     fastcgi_pass unix: /tmp/php-fcgi .sock;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME  /usr/local/nginx/html $fastcgi_script_name;
     }
     access_log  /home/logs/xxx .log combined_realip;
}

combined_realip为nginx.conf 定义的日志格式的名字。


使用curl测试之后,产生新的log;

1
2
3
[root@localhost vhosts] # curl -x127.0.0.1:80 192.168.20.30/index.html -I
[root@localhost vhosts] # cat /home/logs/xxx.log 
127.0.0.1 - [15 /May/2015 :15:13:49 +0800]192.168.20.30  "/index.html"  200 "-"  "curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

错误日志error_log日志级别:

error_log 级别分为 debug, info, notice, warn, error, crit  默认为crit, 该级别在日志名后边定义格式如下:error_log  /your/path/error.log crit;  

crit 记录的日志最少,而debug记录的日志最多。如果你的nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log并没有看到有意义的信息,那么就可以调一下错误日志的级别,当你调成error级别时,错误日志记录的内容会更加丰富。



四、静态文件(图片、flash、js、css)不记录日志,并配置缓存;

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

        {

         expires    30d;

         access_log off;

        }

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

        {

         expires    12h;

         access_log off;

        }

expires      定义缓存时间;

access_log off 不记录日志;

试验测试:touch 1.jpg 2.js文件,使用curl测试cache-control为缓存时间

1
[root@localhost vhosts] # touch /usr/local/nginx/html/1.jpg
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost vhosts] # curl -x127.0.0.1:80 www.111.com/1.jpg -I
HTTP /1 .1 200 OK
Server: nginx /1 .6.2
Date: Thu, 14 May 2015 22:11:45 GMT
Content-Type: image /jpeg
Content-Length: 0
Last-Modified: Thu, 14 May 2015 22:11:41 GMT
Connection: keep-alive
ETag:  "55551d9d-0"
Expires: Sat, 13 Jun 2015 22:11:45 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost vhosts] # touch /usr/local/nginx/html/2.js
[root@localhost vhosts] # curl -x127.0.0.1:80 www.111.com/2.js -I
HTTP /1 .1 200 OK
Server: nginx /1 .6.2
Date: Thu, 14 May 2015 22:11:00 GMT
Content-Type: application /javascript
Content-Length: 0
Last-Modified: Thu, 14 May 2015 22:10:53 GMT
Connection: keep-alive
ETag:  "55551d6d-0"
Expires: Fri, 15 May 2015 10:11:00 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes


五、防盗链

在nginx默认主机default.conf中的server部分中添加如下代码:

 // 对taobao、baidu、google、soso这些域名的网站不进行盗链。如果不是规定的域名,返回403错误;或者跳转到一个自定义的图片上;

    location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {  

      valid_referers none blocked server_names  *.taobao.com *.baidu.com  *.google.com *.google.cn *.soso.com; 

      if ($invalid_referer) {

          return 403;

          #rewrite ^/ http://www.example.com/nophoto.gif;

       }

     }


~* 代表不区分大小写的匹配;


如同一个配置文件中都有~ 匹配的话,只匹配最上面的;把图片文件的缓存和不记录日志代码,放到一个~匹配中都生效;

    location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
         expires 10d;
         valid_referers none blocked server_names *.1.com *.a.com *.b.com *.baidu.com\
         *.google.com *.google.cn *.soso.com ;
         if ($invalid_referer) {
              return 403;
              #rewrite ^/ http://www.example.com/nophoto.gif;
         }
         access_log off;
    }


使用curl -e测试,需要加http://  使用qq.com访问图片显示403错误,使用1.com是200  OK;验证防盗链成功;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost vhosts] # curl -x127.0.0.1:80 -e "http://www.qq.com"  192.168.20.30/1.jpg -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .6.2
Date: Fri, 15 May 2015 08:28:07 GMT
Content-Type: text /html
Content-Length: 168
Connection: keep-alive
[
root@localhost vhosts] # curl -x127.0.0.1:80 -e "http://www.1.com"  192.168.20.30/1.jpg -I
HTTP /1 .1 200 OK
Server: nginx /1 .6.2
Date: Fri, 15 May 2015 08:28:16 GMT
Content-Type: image /jpeg
Content-Length: 0
Last-Modified: Fri, 15 May 2015 07:55:55 GMT
Connection: keep-alive
ETag:  "5555a68b-0"
Accept-Ranges: bytes






本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1652192,如需转载请自行联系原作者

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
1月前
|
缓存 NoSQL Java
Mybatis学习:Mybatis缓存配置
MyBatis缓存配置包括一级缓存(事务级)、二级缓存(应用级)和三级缓存(如Redis,跨JVM)。一级缓存自动启用,二级缓存需在`mybatis-config.xml`中开启并配置映射文件或注解。集成Redis缓存时,需添加依赖、配置Redis参数并在映射文件中指定缓存类型。适用于查询为主的场景,减少增删改操作,适合单表操作且表间关联较少的业务。
|
3月前
|
存储 缓存 安全
在 Service Worker 中配置缓存策略
Service Worker 是一种可编程的网络代理,允许开发者控制网页如何加载资源。通过在 Service Worker 中配置缓存策略,可以优化应用性能,减少加载时间,提升用户体验。此策略涉及缓存的存储、更新和检索机制。
|
3月前
|
存储 缓存 监控
配置 Webpack 5 持久化缓存时需要注意哪些安全问题?
【10月更文挑战第23天】通过全面、系统地分析和应对安全问题,能够更好地保障 Webpack 5 持久化缓存的安全,为项目的成功构建和运行提供坚实的安全基础。同时,要保持对安全技术的关注和学习,不断提升安全防范能力,以应对日益复杂的安全挑战。
|
4月前
|
存储 缓存 监控
|
4月前
|
存储 缓存 负载均衡
Nginx代理缓存机制
【10月更文挑战第2天】
144 4
|
4月前
|
存储 缓存 NoSQL
Nginx缓存
Nginx缓存
40 2
|
5月前
|
缓存 监控 负载均衡
在使用CDN时,如何配置缓存规则以优化性能
在使用CDN时,如何配置缓存规则以优化性能
消息中间件 缓存 监控
220 0
|
4月前
|
缓存 NoSQL 数据处理
原生php实现redis缓存配置和使用方法
通过上述步骤,你可以在PHP项目中配置并使用Redis作为高性能的缓存解决方案。合理利用Redis的各种数据结构和特性,可以有效提升应用的响应速度和数据处理效率。记得在实际应用中根据具体需求选择合适的缓存策略,如设置合理的过期时间,以避免内存过度消耗。
109 0
|
20天前
|
缓存 NoSQL 中间件
Redis,分布式缓存演化之路
本文介绍了基于Redis的分布式缓存演化,探讨了分布式锁和缓存一致性问题及其解决方案。首先分析了本地缓存和分布式缓存的区别与优劣,接着深入讲解了分布式远程缓存带来的并发、缓存失效(穿透、雪崩、击穿)等问题及应对策略。文章还详细描述了如何使用Redis实现分布式锁,确保高并发场景下的数据一致性和系统稳定性。最后,通过双写模式和失效模式讨论了缓存一致性问题,并提出了多种解决方案,如引入Canal中间件等。希望这些内容能为读者在设计分布式缓存系统时提供有价值的参考。感谢您的阅读!
110 6
Redis,分布式缓存演化之路

热门文章

最新文章