Nginx(三)——配置文件

https配置

  • 准备好密钥以及证书
[root@localhost ~]# ls
anaconda-ks.cfg  www.example.com.crt
certificate.sh   www.example.com.key
  • 配置nginx配置文件
[root@localhost conf]# vim nginx.conf
......
    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  www.example.com;   ## 域名与生成证书的域名保持一致

        ssl_certificate      /root/www.example.com.crt;  
        ssl_certificate_key  /root/www.example.com.key;   ##填写对应密钥和证书的路径

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
  • 访问测试

rewrite配置及详解(重写URL)

URL重写有利于网站首选域的确定,对于同一资源页面多条路径的301重定向有助于URL权重的集中

rewrite语法及参数

语法:

rewrite <regex> <replacement> [flag]
关键字 正则表达式 替代内容 flag标记
  • 关键字:固定为rewrite
  • 正则表达式:perl兼容正则表达式语句进行规则匹配
  • 替代内容:将正则匹配的内容替换成replacement
  • flag标记:rewrite支持的flag标记

flag标记说明

flag 作用
last 本条规则匹配完成后,继续向下匹配新的location URI规则
break 本条规则匹配完成即终止,不再匹配后面的任何规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
  • 举例:

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

  1. rewrite为固定关键字,表示开始进行rewrite匹配规则

  2. regex部分是 ^/(.*) ,这是一个正则表达式,匹配完整的域名和后面的路径地址

  3. replacement部分是http://www.test.com/$1,$1是取自regex部分()里的内容。匹配成功后跳转到的URL。

  4. flag部分 permanent表示永久301重定向标记,即跳转到新的 http://www.test.com/$1 地址上

rewrite实例

情况一:location配置

......
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/nginx/html;
            index  index.html index.htm ;
        }
        location = /test {
            rewrite ^/(.*) http://www.baidu.com break;  将URI=/test转到百度首页
}
  • 访问

情况二:server配置

   server {
        listen       80;
        server_name  test.example.com;
        location / {
            rewrite ^/(.*) http://www.example.com break;
        }
}
   server {
        listen       80;
        server_name  www.example.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/nginx/html;
            index  index.html index.htm ;
  • 访问

IF判断

  • 语法:
if (condition) {...}

Nginx常见内置变量

变量名称 变量用途
$args 客户端请求中的参数
$http_cookie 客户端的cookie信息
$http_referer 引用地址
$http_user_agent 客户端代理信息
$http_via 最后一个访问服务器的ip地址
$http_x_forwarded_for 相当于网络访问路径
$remote_addr 客户端ip地址
$remote_port 客户端端口号
$server_port 请求到达的服务器端口号
$server_addr 服务器地址

IF应用场景

基于不同浏览器实现分离

if ($http_user_agent ~ Firefox) {
  rewrite ^(.*)$ /firefox/$1 break;
}

if ($http_user_agent ~ MSIE) {
  rewrite ^(.*)$ /msie/$1 break;
}

if ($http_user_agent ~ Chrome) {
  rewrite ^(.*)$ /chrome/$1 break;
}

通过$http_user_agent的值判断客户代理为哪种浏览器,基于不同浏览器,将重读到不同的URL

防盗链

location ~* \.(jpg|gif|jpeg|png)$ {
  valid_referers none blocked www.testsoft.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.testsoft.com/403.html redirect;
  }
}

通过返回$invalid_referer值来判断是搜索引擎访问,还是直接网址访问.

如果是通过搜索引擎访问,则变量有值,条件成立,跳转403地址。否则不予跳转

区分PC或手机访问不同的网页

location / {
                proxy_pass test.example.com;

        if ( $http_user_agent ~* "(mobile|nokia|iPhone|ipad|android|samsung|htc|blackberry)" )
                {
                rewrite  ^/$    http://www.baidu.com;
                }

                index index.html index.htm;
        }

upstream配置与实例

  • 语法:
  1. http端定义RS列表及监听端口等信息
Syntax:	    upstream name { ... };
Default:	-
Context:	http
  1. server端添加定义代理location
server {
  location / {
    proxy_pass http://name;
  }
}

注意:upstream定义的name与location指定的name名字必须一致

  • 实例:
  1. 环境
主机名 ip
nginx 192.168.197.141
httpd 192.168.197.154
httpd 192.168.197.155
  1. nginx配置
http {
......
    upstream testapache {
        server 192.168.197.154;     添加RS的地址,如果需要添加权重,可加入"weight"参数
        server 192.168.197.155;
   }

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    keepalive_requests  50;
    #gzip  on;
    server {
        location / {
            proxy_pass http://testapache;          ## 定义location
        }
   }
  1. 访问nginx主机IP,以达到负载均衡的效果
[root@www conf]# curl 192.168.197.141
Apache1
[root@www conf]# curl 192.168.197.141
Apache2
[root@www conf]# curl 192.168.197.141
Apache1
[root@www conf]# curl 192.168.197.141
Apache2
[root@www conf]# curl 192.168.197.141
Apache1
[root@www conf]# curl 192.168.197.141
Apache2
posted @ 2020-12-24 18:13  阿不思布丁  阅读(86)  评论(0编辑  收藏  举报