初步学习nginx

  1. nginx的基本配置

    1. Location(难点):location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作。

      1. 一个配置属性:根据用户的请求地址,去服务器上匹配文件
        1. 配置格式: location [符号] 请求地址(模糊地址)
        2. 里面还有一个配置属性: root 文件(目录)路径
    2. 反向代理的配置

      1. 概念:以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

      2. 代理:

        1. 正向代理:客户端-内容链接到外网
        2. 反向代理:让外网可以访问内网的资源
      3. 配置:

        语法规则: location [=|~|~*|^~] /uri/ {}
        
        = 开头表示精确匹配
        
        ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。以xx开头
        
        ~ 开头表示区分大小写的正则匹配                     以xx结尾
        
        ~* 开头表示不区分大小写的正则匹配                以xx结尾
        
        !~!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
        
        / 通用匹配,任何请求都会匹配到。
        
        匹配次序:
        首先精确匹配 =-》其次以xx开头匹配^~-》然后是按文件中顺序的正则匹配-》最后是交给 / 通用匹配。
        
        当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
        
        示例:
        location = / {
           #规则A
        }
        location = /login {
           #规则B
        }
        location ^~ /static/ {
           #规则C
        }
        location ~ \.(gif|jpg|png|js|css)$ {
           #规则D,注意:是根据括号内的大小写进行匹配。括号内全是小写,只匹配小写
        }
        location ~* \.png$ {
           #规则E
        }
        location !~ \.xhtml$ {
           #规则F
        }
        location !~* \.xhtml$ {
           #规则G
        }
        location / {
           #规则H
        }
        那么产生的效果如下:
        
        访问根目录/, 比如http://localhost/ 将匹配规则A
        
        访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
        
        访问 http://localhost/static/a.html 将匹配规则C
        
        访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而 http://localhost/static/c.png 则优先匹配到 规则C
        
        访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。
        
        访问 http://localhost/a.xhtml 不会匹配规则F和规则G,
        
        http://localhost/a.XHTML不会匹配规则G,(因为!)。规则F,规则G属于排除法,符合匹配规则也不会匹配到,所以想想看实际应用中哪里会用到。
        
        访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
        
        

        配置示例:

        #直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
        #这里是直接转发给后端应用服务器了,也可以是一个静态首页
        # 第一个必选规则
        location = / {
            proxy_pass http://tomcat:8080/index
        }
         
        # 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
        # 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
        location ^~ /static/ {                              //以xx开头
            root /webroot/static/;
        }
        location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {     //以xx结尾
            root /webroot/res/;
        }
         
        #第三个规则就是通用规则,用来转发动态请求到后端应用服务器
        #非静态文件请求就默认是动态请求,自己根据实际把握
        location / {
            proxy_pass http://tomcat:8080/
        }
        
        
    3. upstream的配置

  2. 掌握nginx下的session共享问题 (难点)

    1. nginx和两个tomcat集群之后,如果所有的tomcat都运行一个项目。存在session不一致的问题

    nginx 相关命令

    nginx 服务器重启命令,关闭
    nginx -s reload :修改配置后重新加载生效
    nginx -s reopen :重新打开日志文件
    nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确
    
    关闭nginx:
    nginx -s stop :快速停止nginx
    quit :完整有序的停止nginx
    
    其他的停止nginx 方式:
    ps -ef | grep nginx
    kill -QUIT 主进程号 :从容停止Nginx
    kill -TERM 主进程号 :快速停止Nginx
    pkill -9 nginx :强制停止Nginx
    
     
    
    启动nginx:
    nginx -c /path/to/nginx.conf
    
    平滑重启nginx:
    kill -HUP 主进程号
    
  3. nginx.conf配置详解

    ##代码块中的events、http、server、location、upstream等都是块配置项##
    ##块配置项可以嵌套。内层块直接继承外层快,例如:server块里的任意配置都是基于http块里的已有配置的##
    ##Nginx worker进程运行的用户及用户组 
    #语法:user username[groupname]    默认:user nobody nobody
    #user用于设置master进程启动后,fork出的worker进程运行在那个用户和用户组下。当按照"user username;"设置时,用户组名与用户名相同。
    #若用户在configure命令执行时,使用了参数--user=usergroup 和 --group=groupname,此时nginx.conf将使用参数中指定的用户和用户组。
    #user  nobody;
     
    ##Nginx worker进程个数:其数量直接影响性能。
    #每个worker进程都是单线程的进程,他们会调用各个模块以实现多种多样的功能。如果这些模块不会出现阻塞式的调用,那么,有多少CPU内核就应该配置多少个进程,反之,有可能出现阻塞式调用,那么,需要配置稍多一些的worker进程。
    worker_processes  1;
     
    ##ssl硬件加速。
    #用户可以用OpneSSL提供的命令来查看是否有ssl硬件加速设备:openssl engine -t
    #ssl_engine device;
     
    ##守护进程(daemon)。是脱离终端在后台允许的进程。它脱离终端是为了避免进程执行过程中的信息在任何终端上显示。这样一来,进程也不会被任何终端所产生的信息所打断。##
    ##关闭守护进程的模式,之所以提供这种模式,是为了放便跟踪调试nginx,毕竟用gdb调试进程时最繁琐的就是如何继续跟进fork出的子进程了。##
    ##如果用off关闭了master_proccess方式,就不会fork出worker子进程来处理请求,而是用master进程自身来处理请求
    #daemon off;   #查看是否以守护进程的方式运行Nginx 默认是on 
    #master_process off; #是否以master/worker方式工作 默认是on
     
    ##error日志的设置#
    #语法: error_log /path/file level;
    #默认: error_log / log/error.log error;
    #当path/file 的值为 /dev/null时,这样就不会输出任何日志了,这也是关闭error日志的唯一手段;
    #leve的取值范围是debug、info、notice、warn、error、crit、alert、emerg从左至右级别依次增大。
    #当level的级别为error时,error、crit、alert、emerg级别的日志就都会输出。大于等于该级别会输出,小于该级别的不会输出。
    #如果设定的日志级别是debug,则会输出所有的日志,这一数据量会很大,需要预先确保/path/file所在的磁盘有足够的磁盘空间。级别设定到debug,必须在configure时加入 --with-debug配置项。
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
     
    ##pid文件(master进程ID的pid文件存放路径)的路径
    #pid        logs/nginx.pid;
     
     
    events {
     #仅对指定的客户端输出debug级别的日志: 语法:debug_connection[IP|CIDR]
     #这个设置项实际上属于事件类配置,因此必须放在events{……}中才会生效。它的值可以是IP地址或者是CIRD地址。
     	#debug_connection 10.224.66.14;  #或是debug_connection 10.224.57.0/24
     #这样,仅仅以上IP地址的请求才会输出debug级别的日志,其他请求仍然沿用error_log中配置的日志级别。
     #注意:在使用debug_connection前,需确保在执行configure时已经加入了--with-debug参数,否则不会生效。
    	worker_connections  1024;
    }
     
    ##核心转储(coredump):在Linux系统中,当进程发生错误或收到信号而终止时,系统会将进程执行时的内存内容(核心映像)写入一个文件(core文件),以作为调试只用,这就是所谓的核心转储(coredump).
     
    http {
    ##嵌入其他配置文件 语法:include /path/file
    #参数既可以是绝对路径也可以是相对路径(相对于Nginx的配置目录,即nginx.conf所在的目录)
        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;
     
        server {
    ##listen监听的端口
    #语法:listen address:port [ default(deprecated in 0.8.21) | default_server | [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ] ]
    #default_server: 如果没有设置这个参数,那么将会以在nginx.conf中找到的第一个server块作为默认server块
    	listen       8080;
     
    #主机名称:其后可以跟多个主机名称,开始处理一个HTTP请求时,nginx会取出header头中的Host,与每个server中的server_name进行匹配,以此决定到底由那一个server来处理这个请求。有可能一个Host与多个server块中的server_name都匹配,这时会根据匹配优先级来选择实际处理的server块。server_name与Host的匹配优先级见文末。
    	 server_name  localhost;
     
            #charset koi8-r;
     
            #access_log  logs/host.access.log  main;
     
            #location / {
            #    root   html;
            #    index  index.html index.htm;
            #    autoindex on   #自动索引
            #}
     
    ##location 语法: location [=|~|~*|^~] /uri/ { ... }
    # location的使用实例见文末。
    #注意:location时有顺序的,当一个请求有可能匹配多个location时,实际上这个请求会被第一个location处理。
    	location / {
    	proxy_pass http://192.168.1.60;
            }
     
            #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;
        #    }
        #}
     
    }
     
    

小练习:

  1. 实现nginx和至少两个tomcat的负载均衡的集群,要求一台nginx,两台tomcat。并且完成两个tomcat的session共享问题。

    切记:两台服务器时间必须同步

    今天踩的坑:忽略了两台服务器的时间,经检查,服务器时间不一致,session共享问题完美解决

    同步时间:
    yum install ntp
    service ntpd status
    ntpdate time.nist.gov
    
    安装mmemcached
    yum –y install memcached
    启动
    memcached -d -m 256m -p 11211 -l 192.168.200.22 -u root -P /tmp/
    scp -r tomcat7-1 root@192.168.200.21:/usr/local/
    
    在tomcat的conf下的context.xml文件中添加
    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" 
    	memcachedNodes="n1:192.168.200.22:11211" 
        sticky="false" 
        lockingMode="auto"
        sessionBackupAsync="false"
    	requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
        sessionBackupTimeout="1000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" 
    />
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值