zoukankan      html  css  js  c++  java
  • nginx学习

    nginx相关命令

    http://www.cnblogs.com/keefer/p/6188423.html

    nginx配置

    1. 不同目录跳转到不同服务器

    http {
      server {
        location /fm2017/ {   proxy_pass http://localhost:8080/fm2017/;   proxy_redirect default; }
      }
    }

    2. 负载均衡

    http {
        upstream asyou.com{
            server localhost:15794 weight=10; 
        server localhost:40196 weight=10;
        }
        server {
            location / {
                root   html;
                index  index.html index.htm;
            proxy_pass   http://asyou.com; 
            }
        }
    }   

    我用遨游测试的时候,刷新了一百多次,都只是指向一个页面。然后我就把F5按注不放,它就有跳动了,跳不同不服务器。如果我用谷歌测试,谷歌差不多每刷新一次就切换一次服务器。

    感谢博主分享http://blog.csdn.net/e421083458/article/details/30086413

     在测试的时候,我遇到一种情况,就是觉得nginx对服务器宕机发现不了???

        upstream asyou.com{
            server localhost:15794 fail_timeout=100s max_fails=2; 
            server localhost:40196 fail_timeout=100s max_fails=2;
        }

    我就加了两个参数,max_fails=2表示,我只尝试两个,如果两次失败后,我就认为这个服务器宕机了。

    服务器宕机后,一般都不发请求过去了,然后fail_timeout=100s,就是设置它100s再去试一下,看服务器有没有恢复。

    这样我就用谷歌浏览器测吧,

    当一个服务器关了,然后发现刷新一下,一下子加载出来了,再刷新一下,load半天也没反应(应该是切到宕机服务器了),然后点x(停止)。就这样反复几次。

    按理说,好几次load好久都没出来,应该算是失败了,但为什么nginx还是向宕机服务器发请求呢??????不明白

    后来,我发现,原来nginx有一个超时时候,默认的是60s,但是,如果是用户主动点x(停止,就是好久没load出来,用户点停止,放弃),这样nginx并不认为这次请求

    是失败的,因为nginx认为只有60s才算是失败,就算是你等了45s了,然后点了x(停止),nginx也不认为失败。

    所以说,如果客户端设置超时时间为45s,45s后主动放弃,是不是说nginx就会永远发现不了服务器集群中的一台宕机呢?????

    实验如下:

    package cn.angelshelter.fm2017;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Date;
    import org.apache.commons.io.IOUtils;
    
    public class NginxTest {
        public static void main(String[] args) {
            for(int i=0;i<50;i++){
                test();
                try {
                    Thread.sleep(20000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public static void test(){
            URL url = null;
            try {
                url = new URL("http://localhost/pp.html");
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            }
            try {
                URLConnection con = url.openConnection();
                con.setReadTimeout(10000);
                con.setDoOutput(true);
                String s = IOUtils.toString(con.getInputStream());
                //System.out.println(IOUtils.toString(con.getInputStream()));
                System.out.println("成功:" + new Date());
            } catch (IOException e) {
                System.out.println("失败:" + new Date());
                //e.printStackTrace();
            }
        }
    }

    输出:

    成功:Tue Mar 21 16:51:32 GMT+08:00 2017
    失败:Tue Mar 21 16:52:02 GMT+08:00 2017
    成功:Tue Mar 21 16:52:22 GMT+08:00 2017
    失败:Tue Mar 21 16:52:52 GMT+08:00 2017
    成功:Tue Mar 21 16:53:12 GMT+08:00 2017
    失败:Tue Mar 21 16:53:42 GMT+08:00 2017
    成功:Tue Mar 21 16:54:02 GMT+08:00 2017
    失败:Tue Mar 21 16:54:32 GMT+08:00 2017
    成功:Tue Mar 21 16:54:52 GMT+08:00 2017
    失败:Tue Mar 21 16:55:22 GMT+08:00 2017
    成功:Tue Mar 21 16:55:42 GMT+08:00 2017

    结论:

    如果客户端的超时时间小于nginx服务器设置的超时时间(默认是60s)的话,服务器永远发现不了负载均衡组里的宕机服务器。

    又发现一个nginx强大的地方:

    如果 客户端超时时间  >  nginx超时时间  , 这时候超时的话,客户端居然没报错,nginx太强大了,它应该是从服务器a取数据,等了60s没反应,就跑到服务器b去取数据,取了之后

    返回给客户端,客户端无感知,不知道服务器其实是失败了一次。客户端也没报错,就是等得久了点而以,大概要等一个超时时间,这里是60s。所以,把nginx的超时时间设短的话,如果宕了

    一台服务器,另一台即时顶起,客户端无感知,不会报错。

    百度一下,也终于找到修改默认超时时间的参数了:

    proxy_connect_timeout 300s;
    proxy_send_timeout 300s;
    proxy_read_timeout 300s;

    这三个参数是写在location里面的。收工,下班。

    如果  客户端超时时间 < nginx超时时间 , 这时候又超时了的话,客户端会报java.net.SocketTimeoutException: Read timed out错误。

    参数介绍:

    #down 暂时不参与负载
    #weight 权重
    #max_fail 允许请求失败的次数,默认为1
    #fail_timeout max_fail次失败后,暂停的时间
    #backup 所有其他非backup机器down或者忙的时候,才请求backup。所以这台机器压力最轻。

    设置response的header

    location / {
        add_header Access-Control-Allow-Origin *;
    }

     而proxy_set_header sss xxx;是转发给代理服务器的。比如负载均衡有A、B两台服务器,就是转给A或B的,不转给客户端。

    nginx解决跨域问题

    location /baidu/ {
        proxy_pass   https://zhidao.baidu.com/; 
        proxy_redirect default;
    }

    为百度分配一个baidu的子目录,

    这样一来,本为是https://zhidao.baidu.com/list?fr=daohang的网址就可以通过http://localhost/baidu/list?fr=daohang来访问了。

    遇到的问题:

    1. nginx: [emerg] "upstream" directive is not allowed here in C: ginx/conf/nginx.conf:12

    后来检查了一下原来是upstream backend 位置放错了, upstream位置应该放在http模块里面 但必须是在server模块的外面. 

    2. 遇到一种很奇怪的现象,我用localhost作主机名,它可以一段时间访问正常,点刷新4,5下之后,就又打不开了。(我电脑环境,wifi+有线网卡+VPN)

    然后我把配置改为127.0.0.1就一切是正常的。真是奇怪。localhost不是都是解析成127.0.0.1的吗?

    后来到C:WindowsSystem32driversetcHOSTS查看,原来默认的127.0.0.1 localhost是被#

    注释掉了的,对掉#就好了。

    Nginx中文网http://www.nginx.cn/doc/

  • 相关阅读:
    PATA 1071 Speech Patterns.
    PATA 1027 Colors In Mars
    PATB 1038. 统计同成绩学生(20)
    1036. 跟奥巴马一起编程(15)
    PATA 1036. Boys vs Girls (25)
    PATA 1006. Sign In and Sign Out (25)
    读取web工程目录之外的图片并显示
    DOS命令
    java连接oracle集群
    servlet
  • 原文地址:https://www.cnblogs.com/angelshelter/p/6575015.html
Copyright © 2011-2022 走看看