nginx 常用配置

 

多进程模型:master负责管理,worker负责处理连接和http请求
sendfile:从默认的 磁盘--内核(read文件)--用户态--内核(write数据)--协议栈,变成磁盘--内核--协议栈

upstream 定义后端的多个服务器地址

server {
        listen       80 default_server; # 域名不匹配时的默认server
        listen       [::]:80 default_server;
        server_name  static.test.com; # 域名

        access_log  /var/log/nginx/static_access.log  main;
        #设置访问日志文件路径,用于记录每个访问请求,“main”调用上面日志格式

        location / {
            root    /usr/share/nginx/html; #设置WEB应用根目录。
            index index.html;#设置默认首页文件
        }
        error_page 404 /404.html; #当匹配到响应代码,则将请求重定向到指定的URI。
        location = /40x.html {
        #匹配上面重定向的URI,则呈现相关响应代码的会呈现给用户的内容。若"{}"为空则返回默认页面。
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
}


location = / {
    proxy_pass http://tomcat:8080/index # = 精确匹配
}

location ^~ /static/ {   # 以static开头
    root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {     //以xx结尾
    root /webroot/res/;
}
 
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
location / {
    proxy_pass http://tomcat:8080/
}

# 动态代理
http {        #upstream模块包含在http模块下
    upstream  myserver{        #定义upstream名字,下面会引用
        server 192.168.1.100;        #指定后端服务器地址
        server 192.168.1.110;        #指定后端服务器地址
        server 192.168.1.120;        #指定后端服务器地址
    }
 
    server {
        listen 80;
        server name www.myserver.com;
        location / {
            proxy_pass http://myserver;        #引用upstream
        }
    }
}

上一篇:nginx集群


下一篇:nginx配置负载均衡