nginx实战经验

nginx

By rambo

nginx:

  • nginx.exe -s stop //停止nginx
  • nginx.exe -s reload //重新加载nginx
  • nginx.exe -s quit //退出nginx
  • start nginx
  • nginx -s reload -c E:\nginx-1.17.1\conf\nginx.conf //重新加载配置

访问 http://hxdl.com:81/i1510_v2/team/login.jsp 自动转发
172.18.127.58:8099
nginx.conf配置
参考附件nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    upstream  site58  {
        #每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理,
        #可以解决session的问题。如果server挂掉,能自动剔除。
        ip_hash;
        server 172.18.127.58:8099 weight=3 fail_timeout=2s max_fails=1;
        server 192.168.10.58:80 weight=1 fail_timeout=2s max_fails=1;  
    }
 
    server {  
        listen       81;  
        server_name  hxdl.com;  
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        location / {  
            proxy_pass   http://site58;  
            index  index.html index.htm;  
            proxy_connect_timeout 3s;
        }       
    } 

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.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;
    #    }
    #}

}

高阶配置:日志分割、灰度控制


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

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

    log_format log_json escape=json '{"@timestamp":"$time_iso8601",'
                      '"@source":"$server_addr",'
                      '"@nginx_fields":{'
                      '"remote_addr":"$remote_addr",'
                      '"remote_user":"$remote_user",'
                      '"body_bytes_sent":"$body_bytes_sent",'
                      '"request_time":"$request_time",'
                      '"status":"$status",'
                      '"host":"$host",'
                      '"uri":"$uri",'
                      '"server":"$server_name",'
                      '"port":"$server_port",'
                      '"protocol":"$server_protocol",'
                      '"request_uri":"$request_uri",'
                      '"request_body":"$request_body",'
                      '"request_method":"$request_method",'
                      '"http_referrer":"$http_referer",'
                      '"body_bytes_sent":"$body_bytes_sent",'
                      '"http_x_forwarded_for":"$http_x_forwarded_for",'
                      '"http_user_agent":"$http_user_agent",'
                      '"upstream_response_time":"$upstream_response_time",'
                      '"upstream_addr":"$upstream_addr"}}';



    #access_log  E:/nginx_log/access.log  main;
    #使用log_json格式
    #access_log  E:/nginx_log/access.log  log_json;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream  default  {
        #每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理,
        #可以解决session的问题。如果server挂掉,能自动剔除。
        ip_hash;
        #server 172.18.127.58:8099 weight=3 fail_timeout=2s max_fails=1;
        server 192.168.10.58:80 weight=1 fail_timeout=2s max_fails=1;  
    }

    upstream  site58  {
        #每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理,
        #可以解决session的问题。如果server挂掉,能自动剔除。
        ip_hash;
        server 172.18.127.58:8099 weight=3 fail_timeout=2s max_fails=1;
        #server 192.168.10.58:80 weight=1 fail_timeout=2s max_fails=1;  
    }
 

    server {  
        listen       80;  
        server_name  ip160.com;  
  
        charset utf8;  
  
        #access_log  E:/nginx_log/access.log  log_json;  
        #通过cookie灰度控制
        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
                set $year $1;
                set $month $2;
                set $day $3;
        }
        access_log E:/nginx_log/api-access-$year-$month-$day.log log_json;
        set $group default;

        if ($http_cookie ~* "orgId=SHOW1") {
            set $group site58;
        }

        location / {  
            proxy_pass   http://$group;  
            index  index.html index.htm;  
            proxy_connect_timeout 3s;
        }       
    } 



}

必要时可以用 两个nignx + keepalive 心跳保证 主从


记录:

时间作者内容本版
2019-07-01Rambo创建V1.0

苔花如米小,也学牡丹开。

版权声明:本文为原创文章,转载请附上链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值