Nginx上手记录

Nginx是啥

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务

Nginx一些特征概念

  • 反向代理:客户端发送请求到反向代理服务器,反向代理服务器转发请求到目标服务器
  • 负载均衡:单机无法满足需求就增加服务器,并且按照一定的规则将客户端请求分发到各个服务器完成客户端请求过程
  • 动静分离:动态资源和静态资源放在不同服务器,加快相应速度,减少单机服务器压力

Nginx安装(Linux centos7)

参考菜鸟教程:https://www.runoob.com/linux/nginx-install-setup.html

Nginx常用命令(可在nginx/sbin/目录下测试)

 查看nginx版本	
 ./nginx -v 
 
 启动nginx
 ./nginx 
 
 关闭nginx
 ./nginx -s stop
 
 重新加载nginx 
  ./nginx -s reload

Nginx配置文件

主要是三部分组成:

  • 全局块:配置文件到events块之间的内容,设置之后主要影响nginx服务器整体运行的配置指令,包括服务器组,允许生成的worker_processes数,进程pid存放路径,日志存放路径和类型以及配置文件引入
  • events块:主要影响nginx服务器与用户网络连接,包括对多个worker_processes下的网络连接序列化,是否允许同时连接多个网络连接,选取某个事件驱动模型来处理连接请求,worker_processes同时支持的最大连接数
  • http块:主要配置代理,缓存和日志定义等绝大多数功能以及第三方模块的配置

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

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


    # 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反向代理配置

  1. 简单配置一个域名映射ip实例

   server {
       listen       80;
       #修改为你指定的ip地址(域名也可以)
       server_name  192.168.3.2;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;

       location / {
           root   html;
           #添加一个转发地址(可写相应的公网ip或者内网ip)
           proxy_pass	http://192.168.3.2:4000;
           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;
       }


}
  1. 不同URL映射不同服务器地址

   server {
       listen       80;
       #修改为你指定的ip地址(域名也可以)
       server_name  192.168.3.2;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;
   	#正则表达式url简单配置(匹配两个不同的url访问不同的web服务器)
       location ~ /server1/ {
           root   html;
           #添加一个转发地址(可写相应的公网ip或者内网ip)
           proxy_pass	http://192.168.3.2:4000;
           index  index.html index.htm;
       }
      location ~ /server2/ {
           root   html;
           #添加一个转发地址(可写相应的公网ip或者内网ip)
           proxy_pass	http://192.168.3.2:4001;
           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;
       }


}

location语法规则: location [=||*|^~] /uri/ { … }

  • = 开头表示精确匹配
  • ^~ 开头表示uri以某个常规字符串开头,理解为匹配url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static//aa匹配到(注意是空格)
  • ~ 开头表示区分大小写的正则匹配
  • ~*开头表示不区分大小写的正则匹配
  • !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
  • / 通用匹配,任何请求都会匹配到

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求

Nginx负载均衡配置

  http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

	#添加upstream配置,自定义一个属性名称
	upstream myserver{
		ip_hash;#还有轮询,权重,fair
		server 192.168.3.2:8080 weight=1
		server 192.168.3.2:8081 weight=1
	}

    server {
        listen       80;
        server_name  192.168.3.2;

  
        location / {
           	proxy_pass	http://myserver;
        	root   html;
            index  index.html index.htm;
        }

      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

Nginx动静分离

  server {
      listen       80;
      server_name  192.168.3.2;
      #静态文件子路径
      location /www {
      	#静态文件根路径
     	root   /data/;
        index  index.html index.htm;
      }

    
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
  }

Nginx高可用集群配置

需要准备多个nginx实例,以及安装Keepalived
安装教程:Keepalived安装配置参考

Nginx原理简析

  • 两个进程:masterworker进程
    1
    nginx进程的简单流程图:
    客户端发送请求;
    nginx的master接收到客户端请求;
    master进程释放请求信号;
    各个worker进程接受到信号之后进行抢占请求,抢占成功的worker进程完成客户端的请求的之后的操作,比如请求服务端资源或者请求服务端程序;

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值