Nginx浅学

Nginx浅学

1.什么是Nginx?

​ Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务,占用内存小,并发能力强。

2.Nginx作用
2.1 正向代理

​ 用来代理“客户端”,是一个位于客户端和原始服务端之间的一个服务器,为了从原始服务器取得内容,客户端向代理服务器发送了一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。

​ 并且正向代理隐藏了真实的客户端,客户端请求的服务都被代理服务器代替来请求。
在这里插入图片描述

2.2 反向代理

​ 反向代理隐藏真实的服务端,反向代理服务器位于用户与目标服务器之间,但是对有用户而言,即用户直接访问反向代理就可以获取目标服务器的资源。同时,用户也不需要知道目标服务器的地址,也无须在用户端作任何设定。

​ 反向代理通常可用来作为Web加速,即使用反向代理作为Web服务器来降低网络和服务器的负载,提高访问效率。
在这里插入图片描述

2.3 负载均衡

​ 在高并发场景下,将不同客户端的请求打在不同的服务器上,降低单个服务器的请求压力

轮询

​ 将请求轮流发给所有的服务器

加权轮询

​ 将请求按照一定的权重发送给不同的服务器,若有服务器A和服务器B,且其权重为3:2,则代表着请求会以3:2的比例转发给两台服务器

ip绑定

​ 将请求方的ip和服务器进行绑定,一个用户访问的服务器就是固定一台,这种之前会使用在保持session会话的状态,因为session是存在Tomcat中的,用户访问不同的服务器时要重新登陆,但是现在使用redis来存储session,session是可用共享的

3.Nginx的常用命令
#切到sbin目录下
cd /usr/local/nginx/sbin
#开启nginx
./nginx
#安全退出
./nginx -s quit
#停止nginx
./nginx -s stop
#重新加载配置文件
./nginx -s reload
#查看nginx进行
ps -ef | grep nginx
4.配置文件
4.1 配置文件概览
#-------------------------全局块结束---------------------------

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#-------------------------全局块结束---------------------------

#----------------------events 块开始--------------------------
events {
    worker_connections  1024;
}
#----------------------events 块结束--------------------------

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

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen      80 ;
        server_name  localhost;

        #charset koi8-r;

        #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;
        #}
    }
    
#-------------------------HTTP块结束-------------------------


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

}
4.2 全局块

​ 全局块配置nginx全局的指令,一般有如下信息:

  • 运行nginx服务器的用户组
  • nginx进程pid存放路径
  • 日志存放路径
  • 配置文件引用
  • 允许生成woeker process数
#user  nobody;
#允许生成的进程数,默认为1
worker_processes  1;

#错误日志存放的路径,级别
#这个设置可以放进全局块、http块和server块
#级别为 debug、info、notice
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定nginx进程运行文件存放地址
pid        logs/nginx.pid;
4.3 events块

​ events块涉及的参数主要影响Nginx服务器与用户的网络连接,常用的设置信息如下:

  • 是否开启对多 work process下的网络连接进行序列化
  • 是否允许同时接受多个网络连接
  • 选取哪种事件驱动模型来处理连接请求
  • 每个work process可以同时支持的最大连接
events {
	#最大连接数,默认为512
    worker_connections  1024;
    #设置网络连接序列化,防止惊群现象发生,默认为on
    accept_mutex on;
    #设置一个进程是否同时接受多个网络来连接,默认为off
    multi_accept on;
    #事件驱动模型
    use epoll;  #事件驱动模型:select| poll| kqueue| epoll| resig| /dev/poll| eventport
}
4.4 http块

​ http全局块配置的指令包括文件引入、MIME_TYPE、连接超时时间、单链接请求数上限等

http {
	#文件扩展名与文件类型映射表
    include       mime.types;
    #默认文件类型,默认为text/plain
    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"';

	#combined为日志格式的默认值
    #access_log  logs/access.log  main;

	#允许sendfile方式传输文件,默认为off,可以在http块、server块,location块
    sendfile        on;
    #tcp_nopush     on;
	
	#连接超时时间,默认为75s,可以在http、server、location块中
    #keepalive_timeout  0;
    keepalive_timeout  65;

	#是否启动压缩
    #gzip  on;

    server {
        listen      80 ;
        server_name  localhost;

        #charset koi8-r;

        #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;
        #}
    }
4.5 server块

​ 这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。

​ 每个http块中可以包括多个server块,而每个server块就相当于一个虚拟主机,而每个server块也分为全局server块,以及可以同时包含多个location块。

server {
		#监听端口
        listen      80 ;
        #监听的域名
        server_name  localhost;

        #charset koi8-r;
        #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;
        #}
    }
5.配置负载均衡

​ 使用nginx配置负载均衡,需要在http块中,server块前进行设置:

upstream mysur{
	server 127.0.0.1:8080;
	server 127.0.0.2:8080;
}

server{
	...
	location ~*^.+${
	#请求转向mysvr定义的服务器列表
	proxy_pass http://mysvr;
	}
}
5.1 热备

​ 若有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务。

upstream mysvr{
	server 127.0.0.1:8080;
	#热备
	server 127.0.0.2:8080 backup;
}
5.2 轮询

​ nginx默认权重都为1,nginx分发请求比例基本为1:1

upstream mysvr{
	server 127.0.0.1:8080;
	server 127.0.0.2:8080;
}
5.3 加权轮询

​ 根据权重来进行轮询,nginx会给权重大的分发请求多,比例为设置的权重比例

upstream mysvr{
	server 127.0.0.1:8080 weight=;
	server 127.0.0.2:8080 weight=3
}
5.4 ip绑定

​ 一个客户端只能访问固定的服务器

upstream mysvr{
	server 127.0.0.1:8080;
	server 127.0.0.2:8080;
	ip_hash;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值