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

一、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;

[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的代码;

[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;

[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最后一行;

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;

[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为缓存时间

[root@localhost vhosts]# touch /usr/local/nginx/html/1.jpg
[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
[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;验证防盗链成功;

[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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Nginx配置域名跳转,可以根据需要添加server节点到nginx.conf文件中。每个server节点对应一个域名,并设置相应的反向代理规则。例如,假设我们要将abc.com跳转到https://127.0.0.1:90,将sss.abc.com跳转到http://127.0.0.1:2223,可以按照以下方式进行配置: ``` server { listen 80; server_name abc.com; #你的域名 location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass https://127.0.0.1:90; #真正服务端口 } } server { listen 80; server_name sss.abc.com; #你的域名的二级域名 location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:2223; #真正服务端口 } } ``` 其中,每个server节点里的`server_name`指定了对应的域名,`location`指定了对应的请求路径规则,`proxy_pass`指定了真正服务的地址和端口。这样配置后,当用户访问abc.com时,请求将被反向代理到https://127.0.0.1:90;当用户访问sss.abc.com时,请求将被反向代理到http://127.0.0.1:2223。这样就实现了域名跳转。 请注意,域名解析是将域名指向网站空间的IP地址,而不是某个端口。因此,在Nginx配置中,需要指定具体的端口号来进行反向代理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Nginx实现域名跳转](https://blog.csdn.net/weixin_38236026/article/details/99688909)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值