nginx与tomcat(三) nginx.conf解析

. nginx.conf配置文件解析

  1. 主要分为五个部分
    1. 全局设置:main
    2. http设置:http
    3. server设置:server
    4. 匹配url路径:location
    5. 其他配置段:event,upstream
  2. 全局设置
    1. user nginx--------------------------指定运行 nginx workre 进程的用户和组
    2. worker_rlimit_nofile #--------------指定所有 worker 进程能够打开的最大文件数
    3. worker_cpu_affinity----------------设置 worker 进程的 CPU 粘性,以避免进程在 CPU 间切换带来的性能消耗。如 worker_cpu_affinity 0001 0010 0100 1000;(四核)
    4. worker_processes 4----------------worker 工作进程的个数,这个值可以设置为与 CPU 数量相同,如果开启了 SSL 和 Gzip,那么可以适当增加此数值
    5. worker_connections 1000----------单个 worker 进程能接受的最大并发连接数,放在 event 段中
    6. error_log logs/error.log info-------错误日志的存放路径和记录级别
    7. use epoll----------------------------使用 epoll 事件模型,放在 event 段中
  3. http配置
    1. server {}:---------------------------定义一个虚拟主机
    2. listen 80;----------------------------定义监听的地址和端口,默认监听在本机所有地址上,该端口不能被其他程序占用,否则启动不了,最开始80开始的端口
    3. server_name NAME [...];------------定义虚拟主机名,可以使用多个名称,还可以使用正则表达式或通配符。
    4. sendfile on--------------------------开启 sendfile 调用来快速的响应客户端
    5. keepalive_timeout 10---------------长连接超时时间,单位是秒。
    6. send_timeout-----------------------指定响应客户端的超时时间
    7. client_max_body_size 10m---------允许客户端请求的实体最大大小
    8. root PATH---------------------------设置请求 URL 所对应资源所在文件系统上的根目录
    9. location[ = | ~ | ~* | ^~ ] URI { ... }--设置一个 URI 匹配路径
        1. =:精确匹配
        2. ~:正则表达式匹配,区分字符大小写
        3. ~*:正则表达式匹配,不区分字符大小写
        4. ^~:URI 的前半部分匹配,且不实用正则表达式
      1. 优先级:= > location 完整路径 > ^~ > ~ > ~* > location 起始路径 > location /
    10. allow 和 deny-------------------------基于 IP 访问控制,
      1. 如:仅允许 192.168.0.1/192.168.255.255 网段客户端访问:
        1. allow 192.168.0.1/192.168.255.255;
        2. deny all;
    11. rewrite <REGEX> <REPL> <FLAG>--URL 重写,可以使用多种标记
      1. 例子:rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
      2. 可用的 flag:
        1. - last:重写完成后,继续匹配其他 rewrite 规则
        2. - break:重写完成后不再继续匹配
        3. - redirect:返回 302 重定向(临时重定向),客户端对重定向的 URL 发起新的请求
        4. - permanent:返回 301 重定向(永久重定向),客户端对重定向的 URL 发起新的请求
      3. 当我们把前段工程放到nginx的html目录下的时候,我们访问前段工程的时候就需要使用到URL的重写
        1. 例如:我们的工程项目叫test,主页面是test目录下的index.html页面。配置如下:location/{rewrite "^/home/(.+)" /test/$1 last;}
        2. 当我们访问主页面的时候使用127.0.0.1:80/home/index.htnl就OK了。
  4. 反向代理配置
    1. 修改部署目录下conf子目录的nginx.conf文件(如nginx/conf/nginx.conf)内容,可调整相关配置。
  5. 负载均衡配置
    1. 权重轮询
      1. 由于weight不同,则如果访问60次,则大概10次localhost:8080,50次192.168.101.9:8080
    2. ip_hash
      1. 上面的2种方式都有一个问题,那就是下一个请求来的时候请求可能分发到另外一个服务器,当我们的程序不是无状态的时候(采用了session保存数据),这时候就有一个很大的很问题了,比如把登录信息保存到了session中,那么跳转到另外一台服务器的时候就需要重新登录了,所以很多时候我们需要一个客户只访问一个服务器,那么就需要用iphash了,iphash的每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
    3. fair(第三方)
      1. 需要安装第三方的fair插件才能配置使用。按后端服务器的响应时间来分配请求,响应时间短的优先分配。
    4. url_hash(第三方)
      1. 需要安装第三方的url_hash插件才能配置使用。按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

 

. 完整实例

#user  nginx;        #windows下可以不配置,但是linux下必须配置,不然没有访问权限。你使用用户nginx启动nginx,就配置为user nginx

worker_processes  8;#工作线程数量,可以配置为CPU的数量

error_log  logs/error.log;#错误日志输出文件

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;#指定进程ID,方便在部署环境的时候写脚本去管理进程

events {

    worker_connections  1024;#配置的最大并发连接数量

}

upstream tomcats {

server 192.168.1.1:8080  max_fails=3 fail_timeout=3s weight=9;

server 192.168.1.2:8080  max_fails=3 fail_timeout=3s weight=1;

}

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;

    #配置一个虚拟的server服务器

    server {

        listen       8880;#监听端口

        server_name  localhost;#主机名称

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            proxy_pass http://tomcats;

            index  index.html index.htm;

        }

        location /home {

            rewrite "^/home/(.+)" /test/$1 last;

        }

       location /Test {

           proxy_pass http://192.168.1.1:8080/TestServlet;

       }

        #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;

        }

    }

    include servers/*;

}

 

猜你喜欢

转载自blog.csdn.net/qq919694688/article/details/82876941