美文网首页
nginx 第二篇

nginx 第二篇

作者: 你好_请关照 | 来源:发表于2019-08-02 21:12 被阅读0次

本文将继nginx第一篇 继续讲解nginx

1、nignx 服务监控状态部署

【官网说明链接】http://nginx.org/en/docs/http/ngx_http_stub_status_module.html

句法: stub_status;
语境: server, location

[root@web03 /etc/nginx/conf.d]# cat status.conf 
server {
    listen       10.0.0.9:80;
    server_name status.xiaoxi.com;

    location /{
    stub_status;
    }
}

监控状态页面信息:

Active connections: 2
server accepts handled requests
6 6 31
Reading: 0 Writing: 1 Waiting: 1

Active connections: 激活的连接数(总的并发连接数)
server accepts: 已经接收的客户端访问服务端总的链接数量(第一个6)
handled: 已经处理的客户端访问服务端总的链接数量(第二个6)
requests: 接收到用户请求报文的总数量(31 )

Reading: 目前读取用户访问请求头数量
Writing: 目前响应用户访问响应头数量
waiting: 目前在内存/队列中未处理请求报文数量

2、nginx 服务日志信息

【官网说说明】http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

(1)access 访问日志: /var/log/nginx/access.log


log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"';

说明:以上信息定义日志格

 access_log  /var/log/nginx/access.log  main;
$remote_addr     10.0.0.1                    用户访问源地址信息
$remote_user     认证用户名                    认证访问网站用户信息
[$time_local]    [20/Jul/2019:10:31:52 +0800]  用户访问网站时间信息
$request         GET /favicon.ico HTTP/1.1     HTTP请求行信息 
$status          404                           显示状态码信息
$body_bytes_sent 555                           响应报文主体尺寸   (尺寸过大需要考虑是否有盗链情况)  
$http_referer    ???
$http_user_agent Chrome/74.0.3729.131          用户使用什么客户端软件访问网站
$http_x_forwarded_for                    记录用户访问的真是IP地址

(2)error 错误日志:/var/log/nginx/access.log

【错误日志官网说明】http://nginx.org/en/docs/ngx_core_module.html#error_log
配置方法:

error_log  /var/log/nginx/error.log warn;
 2019/07/20 10:31:52 [error] 11763#11763: *6 open() "/html/www/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: www.xiaoxi.com, request: "GET /favicon.ico HTTP/1.1", host: "www.xiaoxi.com", referrer: "http://www.xiaoxi.com/"

3、location 说明

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:    —
Context:    server, location
location = / {
        return 301;
    }
    
    location / {
        return 302;
    }
    
    location /documents/ {
        return 401;
    }
    
    location ^~ /images/ {
        return 402;
    }
    
    location ~* \.(gif|jpg|jpeg)$ {
        return 501;
    }

=: 精确匹配 = /xiaoxi www.xiaoxi.com/xiaoxi 最优先
^~: 优先匹配 ^~ /images www.xiaoxi.com/images/xiaoxi.jpg 优先
~: 模糊匹配 ~ /xiaoxi www.xiaoxi.com/xiaoxi/xiaoxi.html 区分大小写
~: 模糊匹配 ~ /xiaoxi www.xiaoxi.com/xiaoxi/xiaoxi.html 不区分大小写
/目录 路径匹配 /xiaoxi www.xiaoxi.com/xiaoxi/xiaoxi.html 区分大小写
/ 默认匹配 / www.xiaoxi.com/oldgirl 如果所有的都不能匹配,就会匹配默认路径;

异常问题:
第一步: www.xiaoxi.com/ ---> location / --> www.xiaoxi.com/xiaoxi.jpg
第二步: www.xiaoxi.com/xiaoxi.jpg ---> location = /xiaoxi --> www.xiaoxi.com/xiaoxi.jpg

3、网站跳转(rewrite)

【官网说明】http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

方法一:rewrite

        Syntax: rewrite regex                replacement      [flag];
                        匹配需要跳转的信息   跳转成什么地址    标记
        Default:    —
        Context:    server, location, if
        
        跳转的标记信息:
        last: 301 一旦跳转完毕,默认停止后续操作(没有相应信息)  不会再地址栏显示跳转页面地址
        break: 301 一旦跳转完毕,会继续访问页面配置信息
        redirect:  临时跳转  302  
        permanent: 永久跳转  301

方法二:return 配置简单方便

        Syntax:     return code URL;
        Default:    —
        Context:    server, location, if
  server {
           listen            80;
           server_name       rewrite.xiaoxi.com;
           root              /html;
           index             index.html;
           location  ~ ^/break/ {
               rewrite  ^/break/  /test/  break;  --- 有跳转目录吗  有首页文件
           }
           location  ~ ^/last/  {
               rewrite  ^/last/  /test/  last;    --- 不需要必须有跳转目录  不需要有首页文件
           }
           location   /test/ {
               default_type   application/json;
               return 200 'ok';
           }
        }

3.1跳转实践(URI跳转)

[root@web03 ~]# mkdir /html/2014/ccc/bbb -p
[root@web03 ~]# echo "2014_ccc_bbb_2" > /html/2014/ccc/bbb/2.html
[root@web03 ~]# mkdir /html/2018/ccc/bbb -p
[root@web03 ~]# echo "2018_ccc_bbb_2" > /html/2018/ccc/bbb/2.html
 [root@web03 conf.d]# cat ccbb.conf 
 server {
     listen 80;
     location / {
         root /html;
         index index.html;
     }
     location /2014 {
         rewrite ^/2014/(.*)$ /2018/$1 redirect;  #其实是跳转到 /html/2018/html/2018/ccc/bbb/2.html
        #return 302 /2018/ccc/bbb/2.html;
     }

3.2 rewrite 跳转隐藏访问目录

[root@web03 /etc/nginx/conf.d]# mkdir /html/course/11/22/33/ -p
[root@web03 /etc/nginx/conf.d]# echo "Curl docs.etiantian.org" > /html/course/11/22/33/course_33.html
server {
    listen       10.0.0.9:80;
    server_name www.xiaoxi.com;

 location / {
        root /html;
          rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html last;
          #固定rewrite ^/course-(.*)  /course/11/22/33/course_33.html break;
               }

}
重启服务后访问 http://www.xiaoxi.com/course-11-22-33.html

如此使用: last break 做跳转不会显示跳转的地址信息

3.3跳转实践 (URL跳转)

server {
    listen       10.0.0.9:80;
    server_name www.xiaoxi.com;

    location ^~ /bbs{
        root   /html/;
       index  index.html index.htm; 
       rewrite (.*) http://www.xiaoxi.com break;
   }

    location  /{
        root   /html/blog ;
       index  index.html index.htm;
   }

}

3.4 实现URL跳转

 [root@web01 conf.d]# cat rewrite.conf 
       server {
          listen            80;
          server_name       rewrite.xiaoxi.com;
          rewrite  ^/(.*)    http://www.jd.com/$1  permanent;  
       }
       server {
          listen            80;
          server_name       www.jd.com;
          location / {
              root   /html;
              index  index.html index.htm;
          }
       }

3.5 另一种实现 URL跳转的方式

[root@web01 conf.d]# cat rewrite.conf 
       server {
          listen            80;
          server_name       rewrite.xiaoxi.com www.jd.com;
location / {
           root   /html;
           index  index.html index.htm;
           if ($http_host ~* ^rewrite.oldboy.com) {
               rewrite  ^/(.*)    http://www.jd.com/$1  permanent;
           }
       }  
}      

=======================================================

4、HTTP 跳转为HTTPS

1、准备工作: 安装nginx程序时,需要开启ssl模块功能() --with-http_ssl_module

2、配置文件中加载ssl配置信息

【官网说明】 http://nginx.org/en/docs/http/ngx_http_ssl_module.html

       server {
          listen              443 ssl;
          server_name         rewrite.oldboy.com;
          ssl_certificate     /etc/nginx/server.crt;    公钥
          ssl_certificate_key /etc/nginx/server.key;    私钥
       }

3、创建私钥

 先有私钥:
openssl genrsa -idea -out server.key 2048

4、创建证书

 openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

-days 36500 --- 设置证书时效
-x509 --- 设置证书文件信息格式
-sha256 --- 证书数据加密方式
-nodes -newkey --- 去掉密码信息
rsa:2048 --- 识别私钥加密信息
-keyout --- 读取私钥文件
-out --- 输出一个证书

5、重启nginx 测试网站是否是HTTS

6、让网站为HTTPs

访问如下配置的网站时需要加入https如:htttps://www.xiaoxi.com
[root@web03 ~]# cat  /etc/nginx/conf.d/www.conf 
server {
    listen       10.0.0.9:443 ssl;
    server_name www.xiaoxi.com;
    
    ssl_certificate     /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;
location ^~ /{
        root   /html/bbs;
       index  index.html index.htm; 
      # rewrite /bbs/(.*) https://bbs.xiaoxi.com last;
   }
}

7、网站HTTP实现HTTPS跳转

访问如下配置的网站:www.xiaoxi.com;即可自动跳转为https://www.xiaoxi.com
server {
    listen       10.0.0.9:80 ;
    server_name www.xiaoxi.com;
    rewrite (.*) https://www.xiaoxi.com/$request_uri redirect;
}

server {
    listen       10.0.0.9:443 ssl;
    server_name www.xiaoxi.com;

    ssl_certificate     /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;
location ^~ /{
        root   /html/bbs;
       index  index.html index.htm;
      # rewrite /bbs/(.*) https://bbs.xiaoxi.com last;
   }
}

8、nginx跳转时常用的一些内置变量:

Rewrite常用内置变量,在匹配过程中可以引用一些Nginx的全局变量

       $server_name       当前用户请求的域名
       $request_filename  当前请求的文件路径名(带网站的主目录/html/images/test.jpg)
       $request_uri       当前请求的文件路径名(不带网站的主目录/images/test.jpg)
       $scheme            用的协议,比如http或者https

相关文章

网友评论

      本文标题:nginx 第二篇

      本文链接:https://www.haomeiwen.com/subject/pwpqdctx.html