zoukankan      html  css  js  c++  java
  • nginx的配置与安装

     

    说说在Linux系统下安装配置Nginx的详细过程。

    1. 从Nginx官网下载Nginx。目前最新的稳定版为:1.6.2.

    2. 将下载下来的Nginx上传到/opt/nginx目录下。运行“tar -zxvf nginx-1.6.2.tar.gz”进行解压。

    3. 切换到/opt/nginx/nginx-1.6.2目录下,运行./configure进行初始化配置。如出现下面的提示,说明该机器没有安装PCRE,而Nginx需要依赖PCRE,需要手动安装PCRE,见 http://www.linuxidc.com/Linux/2015-03/114986.htm

    详解Linux下安装配置Nginx

    4. 安装完PCRE后,再次运行./configure进行初始化即可。注意这里生成的配置文件,尤其箭头所指的方向,是启动nginx时的路径。

    详解Linux下安装配置Nginx

    5. 运行 make install 进行编译。

    6. 切换到步骤4生成的nginx运行目录下,运行./nginx,启动Nginx。

    如出现下面所述的错误,在已安装PCRE库的情况下,需要配置PCRE共享库。具体操作看这里 http://www.linuxidc.com/Linux/2015-03/114985.htm 。

    ./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

    7.  在浏览器中输入IP:端口号,出现如下图所示,说明安装成功。

    详解Linux下安装配置Nginx

    如出现nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)。可能是端口号被占用,切换到/usr/local/nginx/conf/目录下,更改nginx.conf下的端口号。也有可能是不小心 nginx重启了多次,关闭nginx进程,重启即可。

    详解Linux下安装配置Nginx

    8. 关闭Nginx

    使用 ps -ef|grep nginx 查看nginx的进程,可以看到nginx有两个进程:

    [root@linuxidc sbin]# ps -ef|grep nginx 

    root      7276    1  0 14:21 ?        00:00:00 nginx: master process ./nginx

    www      7277  7276  0 14:21 ?        00:00:00 nginx: worker process

    root      7279  5197  0 14:21 pts/1    00:00:00 grep nginx

    8.1 从容停止nginx

    kill - QUIT nginx 主进程号

    8.2 停止nginx所有进程

    [root@linuxidc sbin]# pkill -9 nginx

    --------------------------------------分割线 --------------------------------------

    CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

    使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

    搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

    CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

    CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

    CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

    Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

    Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

    Nginx 的详细介绍:请点这里
    Nginx 的下载地址:请点这里

    本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-03/114988.htm

    ###################################################################################################################################################################

    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    安装
    ubuntu下

    1
    sudo apt-get install nginx

    启动

    1
    2
    sudo /etc/init.d/nginx start    #通过init.d下的启动文件启动。
    sudo service nginx start#通过ubuntu的服务管理器启动

    配置文件位置

    1
    /etc/nginx/nginx.conf

    编译安装
    1.先决条件

    (1).gcc

    1
    apt-get install gcc

    (2).pcre(Perl Compatible Regular Expression)

    1
    apt-get install libpcre3 libpcre3-dev

    (3).zlib

    1
    apt-get install zliblg zliblg-dev

    (4).openssl

    1
    2
    3
    apt-get install openssl opensll-dev
     
    #如果非apt,可以使用下载包手动编译安装的方式处理

    2.下载包

    www.nginx.net 下载稳定版

    1
    wget http://nginx.org/download/nginx-1.4.4.tar.gz

    3.解压安装

    1
    2
    3
    4
    5
    6
    7
    8
    tar -xzvf nginx-1.4.4.tar.gz
    #默认,安装目录/usr/local/nginx
    ./configure
    make
    make install
     
    #配置
    ./configure --conf-path=/etc/nginx/nginx.conf

    可以配置一些其他选项

    安装后查看下目录下的Configuration summary
    4.init脚本

    需要给nginx建立一个init脚本
    从网上捞一个,放入/etc/init.d/nginx
    推荐编译配置

    1.使用不同prefix,方便指定不同版本,也便于升级

    1
    ./configure --prefix=/usr/local/nginx-1.4.4

    基本操作
    查看帮助

    1
    /usr/local/nginx/sbin/nginx -h

    立即停止进程(TERM信号)

    1
    /usr/local/nginx/sbin/nginx -s stop

    温和停止进程(QUIT信号)

    1
    /usr/local/nginx/sbin/nginx -s quit

    重加载

    1
    2
    /etc/init.d/nginx reload #有init脚本情况下
    /usr/local/nginx/sbin/nginx -s reload #原生

    检测配置文件是否正确

    1
    2
    /usr/local/nginx/sbin/nginx -t #生产路径下的
    /usr/local/nginx/sbin/nginx -t -c /home/ken/tmp/test.conf #可以测试某个临时文件

    HTTP基本配置
    配置说明
    注释,#
    每条指令总是以分好结束(;)
    配置继承:在一个区块中嵌套其他区段,那么被嵌套的区段会继承其父区段的设置
    字符串,可以没有引号,但是如果存在特殊字符(空格,分号,花括号)需要用引号引起
    单位:大小(k/K m/M) 时间值(ms/s/m/h/d/w/M/y 默认s)
    模块提供各种变量值,可以进行读取和赋值(每个模块提供变量列表需要自己去查)
    配置文件目录结构

    /usr/local/nginx/conf/

    - mime.types 一个文件扩展列表,它们与MIME类型关联
    - fastcgi.conf 与FastCGI相关的配置文件
    - proxy.conf 与Proxy相关的配置文件
    - nginx.conf 应用程序的基本配置文件
    - sites/
        |- a.conf #允许给每个单独网站建立一个配置文件
        |- b.conf
        |- dir/
            |- c.conf

    需要在nginx.conf中使用包含命令

    1
    2
    include sites/*.conf;
    include sites/*/*.conf;

    配置文件结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    http { #嵌入配置文件的根部, 一个http里可以配置多个server
     
      server { #声明一个站点
        server_name www.website.com; #监听的主机名
        listen 80; #监听套接字所使用的ip地址和端口号
     
        error_page 404 /not_found.html;
        error_page 500 501 502 503 504 /server_error.html;
     
        index index.html;
     
        root /var/www/website/com/html; #定义文档的根目录
     
        #location, 通过制定的模式与客户端请求的URI相匹配
        location / { #网站的特定位置
        }
        location /admin/ { #网站的特定位置 #
          alias /var/www/locked/; #只能放在 location区段中,为指定路径提供别名
        }
     
        #操作符,匹配时跟定义顺序无关
        location = /abcd { #精确匹配,不能用正则
        }
        location /abc/ { #url必须以指定模式开始,不能用正则
        }
        location ^~ /abcd$ { #吴标致行为,URI定位必须以指定模式开始,如果匹配,停止搜索其他模式
        }
        location ~ ^/abcd$ { #正则匹配,区分大小写
        }
        location ~* ^/abcd$ { #正则匹配,不区分大小写
        }
        location @test { #定义location区段名,客户端不能访问,内部产生的请求可以,例如try_files或error_page
        }
      }
    }
    模块
    模块化
    nginx真正的魅力在于它的模块,整个应用程序建立在一个模块化系统之上,在编译时,可以对每一个模块进行启用或者禁用

    index模块
    定义往回走哪index页

    1
    index index.php index.html /data/website/index.html;

    #可以指定多个,但是ngxin提供第一个找到的文件
    Log模块
    access_log /file/path;
    error_log /file/path error;  #level: debug/info/notice/warn/error/crit
    日志格式

    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/test.log main;
    Real IP模块
    默认编译nginx不包含这个模块

    当通过nginx将用户请求进行转发时,接收请求的应用要拿到用户的真实IP(经转发拿到的是服务器的IP)

    1
    real_ip_header X-Forwarded-For;

    Access模块
    可以禁用ip段

    语法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #如果规则之间有冲突,会以最前面匹配的规则为准
    deny IP;
    deny subnet;
    allow IP;
    allow subnet;
    # block all ips
    deny  all;
    # allow all ips
    allow  all;

    配置一个blockips.conf,然后在nginx.conf中include

    e.g

    1
    2
    3
    4
    location {
      allow 127.0.0.1; #允许本地ip 注意顺序,allow要放在前面
      deny all; #禁止其他ip
    }

    Rewrite模块
    作用:执行URL重定向,允许你去掉带有恶意的URL,包含多个参数(修改)

    利用正则的匹配,分组和引用,达到目的

    break/return/set等

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    if (-f $uri) {
      break
    }
    if ($uri ~ ^/admin/){
      return 403;
    }
    if ($uri ~ ^/search/(.*)$) {
      set $query $1;
      rewrite ^ /search.php?q=$query?;
    }

    例子

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    rewrite ^/search/(.*)$ /search.php?q=$1?;
     
    rewrite ^/user/([0-9]+)/(.+)$ /user.php?id=$1&name=$2?;
     
    rewrite ^/index.php/(.*)/(.*)/(.*)$ /index.php?p1=$1&p2=$2&p3=$3?;

    rewrite语法

    1
    rewrite A B option;

    options:
            last :表示完成rewrite
            break:本规则匹配完成后,终止匹配,不再匹配后面的规则
            redirect:返回302临时重定向,地址栏会显示跳转后的地址
            permanent:返回301永久重定向,地址栏会显示跳转后的地址
    Proxy模块
    默认模块,允许你讲客户端的HTTP请求转到后端服务器

    1
    2
    3
    4
    5
    6
    7
    8
    location / {
      proxy_pass_header Server; #该指令强制一些被忽略的头传递到客户端
      proxy_redirect off; #允许改写出现在HTTP头却被后端服务器触发重定向的URL,对相应本身不做任何处理
      proxy_set_header Host $http_host; #允许你重新定义代理header值再转到后端服务器.目标服务器可以看到客户端的原始主机名
      proxy_set_header X-Real-IP $remote_addr; #目标服务器可以看到客户端的真实ip,而不是转发服务器的ip
      proxy_set_header X-Scheme $scheme;
      proxy_pass http://localhost:8080;
    }

    upstream模块

    1
    2
    3
    4
    5
    6
    upstream up_name {
      server 192.168.0.1:9000 weight=5; #权重
      server 192.168.0.2:9000 weight=5 max_fails=5 fail_timeout=60s; #在60s内,其错误通信超过5次,认为该服务失效
      server 192.168.0.3:9000 down; #服务标记为离线,不再使用
      server 192.168.0.4:9000 backup; #备份服务器,其他全部宕机了才启用
    }

    其他
    配置静态化目录
     

    1
    2
    3
    4
    5
    location /static/
    {
      root /var/www/app/;
      autoindex off;
    }

    负载均衡

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    http {
      include mime.types;
      default_type application/octet-stream;
     
      keepalive_timeout 120;
     
      tcp_nodelay on;
     
      upstream up_localhost {
        server 127.0.0.1:8000 weight=5;
        server 127.0.0.1:8001 weight=10;
      }
     
      server {
        listen 80;
     
        server_name localhost;
     
        location /{
          proxy_pass http://up_localhost;
          proxy_set_header Host $host;
          proxy_set_header X-Real_IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }
     
    }

    控制页面缓存

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    location ~ .(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
      root /opt/webapp;
      expires 24h;
    }
     
    expires 1 January, 1970, 00:00:01 GMT;
    expires 60s;
    expires 30m;
    expires 24h;
    expires 1d;
    expires max;
    expires off;

    nginx的内置变量
    $arg_PARAMETER 这个变量包含在查询字符串时GET请求PARAMETER的值。
    $args 这个变量等于请求行中的参数。
    $binary_remote_addr 二进制码形式的客户端地址。
    $body_bytes_sent
    $content_length 请求头中的Content-length字段。
    $content_type 请求头中的Content-Type字段。
    $cookie_COOKIE cookie COOKIE的值。
    $document_root 当前请求在root指令中指定的值。
    $document_uri 与$uri相同。
    $host 请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称。
    $is_args 如果$args设置,值为"?",否则为""。
    $limit_rate 这个变量可以限制连接速率。
    $nginx_version 当前运行的nginx版本号。
    $query_string 与$args相同。
    $remote_addr 客户端的IP地址。
    $remote_port 客户端的端口。
    $remote_user 已经经过Auth Basic Module验证的用户名。
    $request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成。
    $request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。
    $request_body_file 客户端请求主体信息的临时文件名。
    $request_completion 请求完成
    $request_method 这个变量是客户端请求的动作,通常为GET或POST。包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。
    $request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。
    $schemeHTTP 方法(如http,https)。按需使用,例:
    rewrite ^(.+)$ $scheme://example.com$1 redirect;
    $server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。
    $server_name 服务器名称。
    $server_port 请求到达服务器的端口号。
    $server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
    $uri 请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。

    使用独立目录, 然后include具体配置
    目录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    nginx.conf
    site/
      a.conf
      b.conf
    nginx.conf
     
    http {
     
      .......
      include /etc/nginx/conf.d/*.conf;
      include sites/*.conf;
    }

    gzip on
    加到http模块中, 开启gzip, 注意gzip_types配置得是压缩的资源类型

    nginx.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    http {
     
     
      .....
     
     
      gzip on;
      gzip_min_length 1k;
      gzip_comp_level 5;
      gzip_proxied expired no-cache no-store private auth;
      gzip_types text/plain text/css application/javascript text/javascript application/x-javascript text/xml application/xml application/xml+rss application/json image/x-icon image/png image/jpg image/jpeg application/font-woff;
      gzip_vary on;
    }
    for multi processers
    nginx.conf
     
    worker_processes 4;
    events {
      worker_connections 2048;
      use epoll;
      multi_accept on;
    }
     
    worker_rlimit_nofile 100000;
    static file cache
      location ~* .(?:css|js)$ {
       expires 12h;
       access_log off;
       add_header Cache-Control "public";
       proxy_pass http://127.0.0.1:5000;
       proxy_redirect off;
      }
    proxy pass
      location /
      {
        proxy_pass http://127.0.0.1:8000;
        proxy_pass_header Server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header Host $http_host;
        proxy_redirect off;
      }

    可以设置超时时间

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
        proxy_connect_timeout 500s;
        proxy_read_timeout 500s;
        proxy_send_timeout 500s;
    静态目录 or 文件
      location /movies/ {
        alias /Volumes/Media/Movies/;
        allow all;
      }
     
      location = /abc.txt {
        alias /data/www/static/abc.txt;
        expires 30d;
        access_log off;
      }

    静态站

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    server {
      listen    192.168.1.1:80;
      server_name www.abc.com;
     
      client_max_body_size 1M;
      access_log logs/blog_access.log;
      error_log logs/blog_error.log;
     
      root /data/static_site_dir;
      index index.html;
     
    }

    return
    直接return

    语法

    1
    2
    return http_code;
    return http_code "content";

    e.g.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    location /api/test/ {
      return 403;
    }
     
    location /stat/ {
      return 204;
    }
     
    location /ping/ {
      return 200;
    }

    for mobile
    移动端和网站端互相跳转

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
      location = / {
        try_files $uri @mobile_rewrite;
      }
     
      location ~ ^/(login|register|search|album|404|album/d+|item/d+|topic)$ {
        try_files $uri @mobile_rewrite;
      }
     
     
      location @mobile_rewrite {
     
        if ($http_user_agent ~* "(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino") {
          set $mobile_rewrite perform;
        }
        if ($http_user_agent ~* "^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-)") {
          set $mobile_rewrite perform;
        }
     
        if ($arg_mobile = 'no') {
          set $mobile_rewrite do_not_perform;
        }
     
        if ($arg_mobile = 'yes') {
          set $mobile_rewrite perform;
        }
     
        if ($mobile_rewrite = perform) {
          rewrite ^ http://$server_name/m$request_uri permanent;
          break;
        }
     
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_redirect off;
     
      }
     
     
      location /m/
      {
     
        set $pc_rewrite 1;
        if ($http_user_agent ~* "(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino") {
          set $pc_rewrite 0;
        }
        if ($http_user_agent ~* "^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-)") {
          set $pc_rewrite 0;
        }
        if ($pc_rewrite = 1) {
          rewrite ^/m/(.*)$ http://$server_name/$1 permanent;
        }
     
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_redirect off;
      }
    redirect to www
    server {
      server_name abc.com;
      rewrite ^(.*) http://www.abc.com$1 permanent;
    }
    allow and deny

    访问ip控制

    1
    2
    3
    4
    5
    location /test/ {
      allow 192.168.1.1;
      deny all;
     
    }

    负载均衡
    nginx.conf

    1
    2
    3
    4
    5
    6
    7
    http {
     
      upstream A {
        server 192.168.1.1:5000;
        server 192.168.1.2:5000;
      }
    }

    sites/a.conf

    1
    2
    3
    4
    5
    6
    7
    server {
     
      location / {
        proxy_pass A;
      }
     
    }

    其他

    1
    centos service cmds

    检查配置文件正确性

    1
    service nginx configtest


    重新加载配置

    1
    service nginx reload
  • 相关阅读:
    Python深浅拷贝&垃圾回收&with语句
    面向对象
    三器(装饰器,生成器,迭代器)
    Redis数据类型&优缺点&持久化方式
    localStroge和sessionStorge
    redis哨兵&codis
    Redis分布式锁
    技术点剖析
    【牛客网】牛客练习赛4 A Laptop【树状数组+离散化】
    组合数有关的公式及常用求和【数学--排列组合】
  • 原文地址:https://www.cnblogs.com/try-better-tomorrow/p/5099340.html
Copyright © 2011-2022 走看看