系统相关
首页 > 系统相关> > Nginx配置

Nginx配置

作者:互联网

Nginx配置

基础知识

nginx可监控到后端服务是否存活,若某台服务器宕机,故障的服务会被自动剔除,使用户访问不受影响

nginx.conf配置文件中:
|http:			设定http服务器,利用它的反向代理功能提供负载均衡支持
--|upstream:	主要用于负载均衡;设置一系列的后端服务器
--|server:		主要用于指定主机和端口
----|location:	主要用于对接接口请求并把请求映射到指定的后端服务器

nginx.conf配置文件

#定义Nginx运行的用户和用户组
#user  nobody;

#nginx进程数,建议设置为等于CPU总核心数
worker_processes  1;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#进程pid文件
#pid  logs/nginx.pid;


events {
	#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; 
	#区别:
	#select和poll	是标准的工作模式
	#kqueue和epoll	是高效的工作模式
	#epoll用在Linux平台上,kqueue用在BSD系统中。
    use epoll;
    
    #用于定义Nginx每个进程的最大连接数,默认是1024
    worker_connections  1024;
}

# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
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"'
		      '"upstream_response_time $upstream_response_time"'
		      '"request_time $request_time"';
    #日志文件路径
    access_log  logs/access.log  main;

    #开启高效文件传输模式,sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    sendfile        on;
    
    #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
    #tcp_nopush     on;
	
	#长连接超时时间,单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;
	
	#开启gzip压缩输出
    #gzip  on;
    
    #负载均衡配置
    upstream tomcat-spring{
    
    #weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
    #max_fails=1 fail_timeout=60s 
    #60秒内出现1次错误,认为这个服务器工作不正常,在接下来的60s内Nginx不再去访问这个服务器
	server 192.168.188.134:18080 weight=1 max_fails=1 fail_timeout=60s;
	server 192.168.188.135:18080 weight=1 max_fails=1 fail_timeout=60s;
	
	#interval检测间隔时间,单位为毫秒
	#rise=2 rise表示请求2次正常的话,标记realserver的状态为up
	#fall=1 fall表示请求1次失败的话,标记realserver的状态为down
	#timeout为超时时间,单位为毫秒
	check interval=1000 rise=2 fall=1 timeout=1000 type=tcp;
    }
	
	#虚拟主机的配置
    server {
    	#监听端口
        listen 80;
        #域名可以有多个,用空格隔开
        server_name  localhost;

        #charset koi8-r;
	
	#拦截spring工程的接口
	location ^~/spring{
		#映射,代理名为tomcat-spring
		proxy_pass http://tomcat-spring;
	}

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

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

负载均衡策略

nginx的upstream目前支持5种方式的分配
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend {
    server 192.168.188.134:18080 weight=1;
    server 192.168.188.135:18080 weight=2;
}
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
例如:
upstream bakend {
    ip_hash;
    server 192.168.188.134:18080;
    server 192.168.188.135:18080;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块
例如:
upstream backend {
    server 192.168.188.134:18080;
    server 192.168.188.135:18080;
    fair;
}
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
例如:
upstream backend {
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
}

nginx对服务巡测规则

1、max_fails=1 fail_timeout=60s 
    60秒内出现1次错误,认为这个服务器工作不正常,在接下来的60s内Nginx不再去访问这个服务器
规则1:有用户请求触发此规则

2、check interval=1000 rise=2 fall=1 timeout=1000 type=tcp;
	interval检测间隔时间,单位为毫秒
	rise表示请求2次正常的话,标记realserver的状态为up
	fall表示请求1次失败的话,标记realserver的状态为down
	timeout为超时时间,单位为毫秒
规则2:nginx自巡测规则	

日志格式设置参数

$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
$upstream_response_time:请求过程中,upstream响应时间
$request_time:整个请求的总时间

标签:index,http,配置,server,Nginx,html,upstream,服务器
来源: https://blog.csdn.net/weixin_48726433/article/details/123194655