Windows 下Nginx 负载均衡配置

1、下载windows版本的nginx文件   下载地址:点击打开链接

2、下载解压


打开 conf 文件夹 配置 nginx.conf 文件


我配置的网站站点:酷点击打开链接

配置如下:



#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;
 
    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;
 
     gzip  on;  #开启网站压缩
 
      upstream  www.coolhoting.com{   #配置主域名
      ip_hash;                                           #负载方式 iphash的方式 实同一个id的用户分发至同一个后端服务器上
      server 172.16.0.2:801;                      #后端服务网站    我用的IIS 站点
      server 172.16.0.2:802; 
      }
    server {
        listen       80;                                          #监听 80端口

        server_name  www.coolhoting.com:80;

        #配置ssl 时 下面是为http 自动跳转到https    当未配置ssl安全连接时可以注释

        #rewrite ^(.*) https://$server_name$1 permanent;

        
        #charset koi8-r; 
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
            autoindex on;
            proxy_pass http://www.coolhoting.com; 
    proxy_set_header   Host             $host:80;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header Via    "nginx";
            client_max_body_size 100m;
            #proxy_connect_timeout 1; 
            #proxy_read_timeout 1; 
            #proxy_send_timeout 1;
        }
           
       location ~ ^/(WEB-INF)/ { 
       deny all;
        }
        #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;
        #}

    }

#nginx 可以设置多个server 实现nginx下多个二级域名的实现

   server {
    listen 80;       # 共用80端口
    server_name m.coolhoting.com;   #设置二级域名
    access_log  logs/blog-site.log;      #日志记录

    location / {
           proxy_redirect off;
           proxy_set_header Host $host;
           proxy_set_header X-Real-Ip $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_pass http://172.16.0.2:805;              # 跳转的服务站点端口等
          }
   }

 
    # HTTPS server    开启配置 ssl  44是端口
     server {
        listen       443 ssl;
        server_name  www.coolhoting.com;
 
        ssl_certificate      1_coolhoting.com_bundle.crt;   # nginx的证书   证书默认放在 conf文件夹下
        ssl_certificate_key  2_coolhoting.com.key;


        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
       #ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #尽量这样设置
        ssl_prefer_server_ciphers  on;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass      http://www.coolhoting.com;  #好像是关键的一个  有些说这个不用设置 但是不设置的话似乎 nginx无                                                                                      法请求到后端服务站点
        }
    }

# 当配置二级源码是2也要配置相应的二级域名的 443端口  不然 二级域名会自动跳转的一级域名网站 
    server {
        listen       443 ssl;
        server_name  m.coolhoting.com; 
        ssl_certificate      1_m.coolhoting.com_bundle.crt;
        ssl_certificate_key  2_m.coolhoting.com.key;

        ssl_session_cache    shared:SSL:1m;

        ssl_session_timeout  5m;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
       #ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers  on;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass      http://m.coolhoting.com; 
        }
    }

}

配置过程中可能出现的问题

1、配置好的 但内网可以访问,外网不能访问  

     可能是服务器防火墙问题   让防火墙通过nginx.exe 就可以了

2、如果报错 :[emerg] 3312#5488: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

说明80 端口占用

也就是80端口已经被占用 杀死进程就好   sudo fuser -k 80/tcp

或者是系统 system 占用了80端口


解决的办法就是让系统进程不再占用80端口,需要修改注册表。

找到:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTP


原来是 3  改为 0  重启电脑系统  

在重启nginx服务即可!


猜你喜欢

转载自blog.csdn.net/lingdu257/article/details/80621353