zoukankan      html  css  js  c++  java
  • nginx 使用总结

    nginx安装: http://www.runoob.com/linux/nginx-install-setup.html

     我按照上面步骤安装的.然后练习下nginx的日常操作,先熟悉下面几个命令

    /usr/local/webserver/nginx/sbin/nginx -t                   # 修改conf后验证

    /usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件

    /usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
    /usr/local/webserver/nginx/sbin/nginx -s stop              # 停止 Nginx

    sudo nginx -c /usr/local/etc/nginx/nginx.conf   # 停止后的启动

        ps -ef |grep nginx

     


    netstat  -anp  |grep  3306

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

    1.设置并返回参数
    location / {


    default_type text/html;

      set $a "hello";
      set $b "world";
      return 200 "$a$b";
    }

    2.获取参数并处理

    获取请求的word参数

    location /test {
    default_type text/html;
    set $suff "s";
    set $word "$arg_word";
    return 200 "单词$word 的复数是$word$suff";
    }

    3.花括号的使用

    location /test1 {
    default_type text/html;
    set $word "$arg_word";
    return 200 "test1单词$word 的复数是${word}s";
    }

    4.http下的geo模块

    可根据请求的url中name参数的IP匹配返回值

    geo $arg_name $a {
    default “我是geo默认值”;
    127.0.0.1 “我是张三”;
    192.168.1.1 “我是李四”;
    }

    location / {
    default_type text/html;
    return 200 $a;
    }

    5.内置动态变量

    目前在nginx的http模块中有六种内置动态变量,分别是“http_”、“sent_http_”、“upstream_http_”、“upstream_cookie”、“cookie_”,“arg_”

    以“http_”开头的动态内置变量可以表示http请求过程中的任意请求头,使用的过程中不区分大小写,并且请求头中如果有“-”字符需要用“_”字符替代。 

    location /gethead {
    default_type text/html;
    return 200 "User-Agent:$http_user_agent";
    }

    6.根据请求头中值判断转发

     

    upstream oldserver {
      server www.baidu.com;
    }
    upstream newserver {
      server www.qq.com;
    }

     

     

    location /livenet {
       set $vc "$http_vc";
         if ( $vc ~* "(200)") {
        proxy_pass http://oldserver;
      }
      if ( $vc ~* "(300)") {
        proxy_pass http://newserver;
      }

     

     



        location /livenet/ {
      include /etc/nginx/proxy.conf;
      proxy_set_header Cookie $http_cookie;
      #根据请求头跳转
      set $vc "$http_vc";
      if ( $vc ~* "(220)") { 
        proxy_pass http://new;
          }

          #根据url请求地址跳转
        if ($request_uri ~* "getinfo"){ 
            proxy_pass http://new;
        }
           proxy_pass http://tomcat;
      }

     

               

             //匹配到xxl后将会在root访问目录后追加xxl

               location ^~ /xxl{
                    root /usr/share/nginx/html/XXL;
                    index index.html index.htm;
              }


    }

     

     

    2.rewrite 使用

    server {

            listen 80;

            server_name abc.com;

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

    }

    https://www.jb51.net/article/82168.htm

    https://www.cnblogs.com/czlun/articles/7010604.html

    其它:

    1. nginx中使用“$”或“${}”符号来表示一个变量

    2. nginx中的变量支持变量插入,比如“I am a $uri”

    3. 可以表示变量的有效字符只有四种:“a-z”、“A-Z”、“0-9”、“_”

    4. nginx中变量可分为内置变量(比如$uri)和自定义变量(比如用set定义的变量)

    5. nginx 中所有的变量都是全局可见的,但它又不是全局变量

    6. nginx中有六种动态内置变量,分别是“http_”、“sent_http_”、“upstream_http_”、“upstream_cookie”、“cookie_”,“arg_”。(在nginx的1.13.2版本中又多一个“$sent_trailer_”)

    7. nginx中几乎所有的内置变量都是不可变的,除了“args”和“$limt_rate”

    8. nginx中所有的变量都会关联一个get_handler()方法,不可缓存的变量每次获取值时都会调用这个方法,可缓存的变量只会调用一次

    9. nginx中的变量在各个请求之前是相互隔离的(主子请求除外)

    10. 变量在主子请求之间是共享的,但最终值是否相同则取决于该变量是否可缓存

    11. nginx中的变量值都是字符型的(除了“${binary_remote_addr}”变量)

    7.启用GZIP

     gzip on;
     gzip_min_length 1k;
     gzip_buffers 4 16k;
     #gzip_http_version 1.1;
     gzip_comp_level 6;
     gzip_types text/plain application/javascript text/javascript image/jpeg image/gif image/png text/css application/xml;
     gzip_vary on;

    注意:application/javascript ,去年x-

    8.配置ssl

    server {
    listen 80;
    listen 443 ssl http2;  //开启http2
    server_name www.your-domain.com your-domain.com;
    index index.php index.html;
    root /data/wwwroot/web;

    ssl_certificate /path/to/your-domain.crt;
    ssl_certificate_key /path/to/your-domain.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #允许的协议
    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; #加密算法(CloudFlare 推荐的加密套件组)
    ssl_prefer_server_ciphers on; #优化 SSL 加密套件
    ssl_session_timeout 10m; #客户端会话缓存时间
    ssl_session_cache builtin:1000 shared:SSL:10m; #SSL 会话缓存类型和大小
    ssl_buffer_size 1400; # 1400 bytes to fit in one MTU

    #add_header X-Frame-Options SAMEORIGIN; #拒绝被嵌入框架(iframe …)
    #add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"; #强制 SSL(包含子域名),并加入 hsts 列表
    add_header X-UA-Compatible "IE=edge,chrome=1"; #IE 用最新内核渲染页面

    if ($host != 'www.your-domain.com' ) { return 301 https://www.your-domain.com$request_uri; } #把根域名 301 到 www 开头的域名
    if ($ssl_protocol = "") { return 301 https://$host$request_uri; } #如果非 ssl 就 301 到 https

    access_log /data/wwwlogs/your-domain.com_nginx.log combined;

    #error_page 404 /404.html;

    if ( $query_string ~* ".*[;'<>].*" ){
    return 404;
    }

    location ~ [^/].php(/|$) {
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }

    location ~ .*.(gif|jpg|jpeg|png|js|css|ico)$ {
    expires 30d;
    }
    }


    chrome 51 以上要使用 openssl 1.0.2 来编译才支持 ALPN 来协商 HTTP2 ,
    可以使用 openssl 1.1.0 版本(目前是测试版) ,或者 LibreSSL 来支持 chacha20 加密套件

    nginx 可以参考上面的这样配置, 80 和 443 端口不用分开两次写。

  • 相关阅读:
    golang ---cron
    Maven私库安装与配置
    Java8新特性之重复注解(repeating annotations)浅析
    String split
    如何将xml转为python中的字典
    json字符串和dict互转
    为什么空格拷贝到linux 会变成两个
    python之socket编程
    ntpdate设置
    Nginx配置ssl证书
  • 原文地址:https://www.cnblogs.com/lpcyj/p/9131140.html
Copyright © 2011-2022 走看看