反向代理及请求负载均衡Nginx开发搭建

来源:互联网 发布:炒股什么软件好 编辑:程序博客网 时间:2024/06/05 18:48

由于是开发使用,本文使用windows版本Nginx搭建,
一,安装
1,从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最新版本。
2,下载后解压。

二, 启动和停止Nginx

1,使用默认的配置文件(nginx-1.11.1\conf\nginx.conf)时,双击“nginx-1.11.1\nginx.exe”,会默认加载nginx-1.11.1\conf\nginx.conf配置文件。等同于命令:nginx -c conf\nginx.conf.

2,使用自定义配置文件启动时,进入nginx的解压目录,cmd进入dos命令窗口加载指定配置文件:
nginx -c conf\myconfig.conf.

Nginx start

3,修改配置文件后应重启生效,可以使用命令 nginx -s reload

4,停止Nginx:nginx -s stop

三,配置文件的详细配置
所有配置均在nginx-1.11.1\conf\nginx.conf文件中配置

1,负载均衡配置

nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

另外一种方式是ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

  upstream request-servers {       #ip_hash;       server 127.0.0.1:8389 weight=10;       server 127.0.0.1:8390 weight=1;   }

weight为权重大小,相对值越大权重越高

2,反向代理设置

  server {         #监听端口,用于http请求真实使用的端口号        listen       80;        #设置http请求时使用的链接名称        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {        #设置主机头和客户端真实地址,以便服务器获取客户端真实IP           proxy_set_header Host $host;           proxy_set_header X-Real-IP $remote_addr;           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;           #禁用缓存           proxy_buffering off;           #反向代理的地址           proxy_pass http://request-servers;        }    }

3,完整配置

#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;    upstream request-servers {       ip_hash;       server 127.0.0.1:8389 weight=10;       server 127.0.0.1:8390 weight=1;   }    server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {        #设置主机头和客户端真实地址,以便服务器获取客户端真实IP           proxy_set_header Host $host;           proxy_set_header X-Real-IP $remote_addr;           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;           #禁用缓存           proxy_buffering off;           #反向代理的地址           proxy_pass http://request-servers;        }        #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;    #    }    #}}

四,测试
本次测试启动了两套服务,http调用端口分别为:8390,8389
分别测试:
8389

8390

通过Nginx后可以直接使用server_name:localhost+监控端口80访问(80端口可以省略不写):
80,Nginx根据负载和权重调整调用。

0 0
原创粉丝点击