• 一个高性能的 HTTP 和反向代理服务器,特点是占用内存少,并发能力强
  • Nginx是一个Web服务器,但不能作为Servlet容器独立运行
  • 专为性能优化而开发,性能是其最重要的要求,十分注重效率,有报告 nginx 能支持高达 50000 个并发连接数
  • 命令

    • start nginx;
    • nginx -s quit
    • nginx -s reload

      反向代理

  • 代理服务端

    动静分离

    • 图片、css、js等交给nginx处理。nginx处理不了的,如jsp 交给tomcat处理
    • nginx处理静态内容的吞吐量很高,比tomcat高多了,可以提升性能

      负载均衡 Session 问题

  • 解决1

    • 通过 IP 地址标记用户,如果多次请求都是从同一个 ip 来的,就都分配到同一个 tomcat
    • 在 upstream 最后加上 ip_hash; 就行了
    • 问题
      • 大量请求来自某个局域网,那么相当于没有负载均衡了
      • 如果之前的服务挂了,nginx只能把请求交给另一个服务,依然存在 session 问题
  • 解决2

    • Redis + tomcat-session-manager
    • 用Redis存储用户信息

      nginx.conf

  • location[ = | ~ | ~* | ^~] url{} 指令说明

    • =,用于不含正则表达式的 url,严格匹配
    • ~,url 包含正则表达式,区分大小写
    • ~*,url 包含正则表达式,不区分大小写
    • ^~,不含正则表达式的 url,寻找匹配度最高的 location
    • 如果有 url 包含正则表达式,不需要有 ~ 开头标识 ```nginx

全局配置,文件开始到 events 之间

user nobody;

一般设置与 cpu 个数相等,也可配置为 auto

worker_processes 1;

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

影响 Nginx 服务器与用户的网络连接

events {

  1. # 单个后台 worker process 进程的最大并发连接数
  2. 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;

  # 指定 nginx 是否调用 sendfile函数来输出文件
sendfile        on;
#tcp_nopush     on;

  # 配置超时时间
#keepalive_timeout  0;
keepalive_timeout  65;
  # 影响散列表的冲突率,越大,会消耗更多内存,但提升检索速率
  #types_hash_max_size 2048;
  # 客户端上传的 body 的最大值
  #client_max_body_size 50m;

#gzip  on;

# 负载均衡,配置多个服务器
  # weight 表示权重
  upstream tomcat_8111_8222 {
        server 127.0.0.1:8111 weight=1;
        server 127.0.0.1:8222 weight=2;
  }

server {
        # 端口号
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
              # 页面存放位置
        root   html;
              # 欢迎页面
        index  index.html index.htm;
    }
        # 反向代理
        location / {
              proxy_pass http://tomcat_8111_8222
        }
        # 动静分离,表示所有的css、js、png访问都由nginx来做
        location ~\.(css|js|png)$ {
              root C:/Users/X7TI/Downloads/tomcat_8111/webapps/ROOT;
        }

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

}

```