nginx配置文件详解

本文详细解释了Nginx的配置文件中各项参数,如工作进程数、错误日志设置、事件模块、HTTP协议配置、upstream负载均衡、server和location块以及HTTPS配置。特别关注了proxy_pass指令在URL路径处理中的作用。
摘要由CSDN通过智能技术生成
#----------------------------------------全局配置----------------------------------------
#user  nobody;
#工作进程数  一般设置的个数与cpu数量一样
worker_processes  1;
 
#错误日志存放位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid文件位置
#pid        logs/nginx.pid;

#----------------------------------------事件部分----------------------------------------

#配置nginx服务器的事件模块相关参数
events {
    worker_connections  1024;
}
#----------------------------------------HTTP部分----------------------------------------
#http/https 协议相关配置段
http {
#----------include指令----------
	# include指令:用于引入其他的子配置文件,可以将一些通用的配置项提取到文件中,然后引入进来
    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----------
	#sendfile 指令是用于将文件内容发送到客户端的指令。它可以让 Nginx 直接将文件内容发送给客户端,而不需要将文件内容先读入内存再发送。这样可以减少 CPU 和内存的使用,提高文件传输的效率
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
	#----------keepalive_timeout指令----------
	#nginx与上游长连接最大空闲时间
    keepalive_timeout  65;

    #gzip  on;
	
	#----------upstream配置----------
	upstream xiaoquserver{
		#ip_hash    #请求根据访问的ip进行hash计算结果分配,这样每个ip就只能访问固定的后端服务器
		#fair 按照后端响应时间分配请求,响应快的优先分配
		server 47.94.212.4:8081   weight=2;  #负载均衡权重
		server 47.94.212.4:8082  weight=1; 
		server 47.94.212.4:8083  down;  # down 标识当前的server不参与负载
		server 47.94.212.4:8082  backup; #backup 其他所有非backup机器忙或者down了的时候,请求backup的服务器
		
		#hash $request_uri  #url_hash  按照url的hash结果来分配请求,使每个url分配到固定的服务器。
		#hash_method crs32;
   }
	
	#----------server块----------
   	#server块 用于配置HTTP服务器的具体行为,包括监听的端口,虚拟主键的配置,请求处理逻辑等
    server {
        listen       80;
		#----------server_name----------
		#用于设置虚拟主机服务名称   有多个时中间用空格分隔。可以值精确匹配、通配符模式配置、正则表达式配置
        server_name  192.168.96.38;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		#----------location块----------
		#location 块,用于指定不同url请求的处理方式,例如静态文件的服务,方向代理等,可支持模糊匹配(location /abc 可以支持链接中包含abc的)、精确匹配(location = /abc 则只能匹配以abc结尾的)、正则表达式
        location / {
            root   html;
			
			#----------proxy_pass----------
			#proxy_pass最后有/,会把匹配location里的路径去掉,截取后面的URL PATH进行转发,
			#例如 location /abc  proxy_pass http://127.0.0.1:8848/  前端请求为192.168.96.38/abc/test01  实际会转发到http://127.0.0.1:8848/test01  去掉abc 这个字段
			#proxy_pass最后没有/,不把匹配location里的路径去掉,
			#例如 location /abc  proxy_pass http://127.0.0.1:8848/  前端请求为192.168.96.38/abc/test01  实际会转发到http://127.0.0.1:8848/abc/test01  保留abc 这个字段
			
			proxy_pass   http://127.0.0.1:8848;
            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;
	#配置https证书
    #    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;
    #    }
    #}

}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值