分布式之Nginx

分布式之Nginx

    • 什么是nginx
    • 为什么不用apache
    • 安装(windows版本,linux版本就不介绍了,请自行百度)
    • 配置文件介绍
      • 结构
      • 反向代理
      • 自动轮询

什么是nginx

百度是这么介绍nginx的,“一个高性能的HTTP和反向代理web服务器”,通俗的讲,你可以理解为就是一个和apache差不多的软件,利用nginx可以做负载均衡,分发,转发,反向代理,限流等。

为什么不用apache

1:apache比较笨重,而nginx以轻量闻名。
2:比apache更好的处理高并发问题。

安装(windows版本,linux版本就不介绍了,请自行百度)

1.下载地址:http://nginx.org/en/download.html
建议选择Stable version版本,即稳定版本
分布式之Nginx_第1张图片
2.下载完以后解压进入文件夹,双击nginx.exe分布式之Nginx_第2张图片
3.在任务管理器中看到进程在运行说明是成功了
分布式之Nginx_第3张图片
4.常用命令
打开命令行工具win+R -> cmd -> 回车键,定位到文件夹目录
分布式之Nginx_第4张图片
nginx -t 检查配置文件是否存在语法错误
nginx -s reload 重新启用加载配置文件
nginx -s reopen 重新打开日志文件
nginx -s stop 停止服务
nginx -s quit 退出服务

配置文件介绍

结构

打开conf/nginx.conf文件(可以用编辑器打开,也可以用记事本打开)

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server  #server块
    {
      ...
    }
    ...     #http全局块
}

配置案例

#全局块
#user  nobody;  #运行nginx的服务器用户/用户组
worker_processes  1; #worker process数量

#错误日志的存放路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; #Nginx进程PID存放路径

#events
events {
    worker_connections  1024; #最大连接数的配置
}


#http块
http {
    include       mime.types; #MIME-Type类型
    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; #超时时间
    #keepalive_requests 10; #单连接请求数上限

    #gzip  on;

    #server块
    server {
        listen       80; #监听的端口
        server_name  localhost; #监听的主机

        #charset koi8-r;

        #access_log  logs/host.access.log  main; #自定义服务日志
	
	#监听url
        location / {
            proxy_pass   http://localhost:3000/; #转发地址
	    #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;
    #    }
    #}
}

反向代理

在对应的server块中配置需要监听的端口,主机
在location块中配置需要指向的真实地址
比如用户请求的是127.0.0.1:8080/info,但是真实数据在123.0.0.1:3000/info的服务器中,那么你可以这么配置:

server {
        listen       8080; #监听的端口
        server_name  127.0.0.1; #监听的主机	
        location /info {
            proxy_pass   http://123.0.0.1:3000/info; 
        }
}

保存好文件,然后命令行中nginx -t一下,检查是否有语法错误,如果有有则会报错。
接着在命令行中nginx -s reload,配置文件就生效了,当你在浏览器中请求127.0.0.1:8080/info时,服务器会将http://123.0.0.1:3000/info的信息展示给页面上了。

自动轮询

一般我们会准备两台服务器,如果一台服务器宕机了,我们可以立即用到备用服务器,那么你在nginx.conf中可以这么配置

    # 注意,这里的地址不要加http/https
    # 还可以给它设置权重
    upstream myHttpServers {
	    server 127.0.0.1:3000 weight=3;
	    server 123.0.0.1:3000 weight=1;
    }

    #server块
    server {
        listen       80; #监听的端口
        server_name  localhost; #监听的主机	
        location /order/list {
            proxy_pass   http://myHttpServers/order/list; #转发地址
        }
    }

今天就整理到这里了,本文会持续更新的,谢谢阅读。

你可能感兴趣的:(分布式系列)