• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
夕颜
博客园    首页    新随笔    联系   管理    订阅  订阅
Windows环境下配置Nginx服务实现负载均衡

1.下载Nginx

下载地址:nginx: download

2.安装Nginx

PS:Nginx基本目录如下:

●conf:存放Nginx配置文件的目录
●docs:存放Nginx文档的目录
●html:存放静态html文件的目录
●logs:存放Nginx日志的目录
●temp:存放临时文件的目录

 

2.1 启动Nginx

nginx 相关命令:

start nginx 启动nginx
nginx -s stop 快速关闭nginx
nginx -s quit 完整关闭nginx,保留操作日志
nginx -s reopen 重新打开日志文件
nginx -s quit 完整有序的停止nginx
nginx -v 查看nginx版本
nginx.exe -s reload 配置完参数后重启
tasklist |findstr "nginx.exe" 查看nginx进程
taskkill /f /t /im nginx.exe 杀死所有nginx进程

2.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 process进程的最大并发链接数,默认1024,可以按照最大客户端连接数max_clients配置
    #nginx作为http服务器的时候:
    #max_clients = worker_processes * worker_connections

    #nginx作为反向代理服务器的时候:
    #max_clients = worker_processes * worker_connections/4
    worker_connections  1024;
}

#http服务器配置,做web服务器或反向代理
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; #开启高效传输模式

    #在nginx中,tcp_nopush配置与tcp_nodelay“互斥”。它可以配置一次发送数据包的大小。
    #也就是说,数据包累积到一定大小后就发送。
    #在nginx中tcp_nopush必须和sendfile配合使用。
    #tcp_nopush     on;

    #连接超时时间 单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;   #启用gzip压缩

    upstream apiservice{ 
        #负载的一个实例,可以是外网IP
        #默认轮询,把每个请求按顺序逐一分配到不同的server,如果server挂掉,能自动剔除。

        #其他配置:
        #1.在负载的实例后面加weigth参数,表示权值,权值越高被分配到的几率越大
        #譬如server 127.0.0.1:71 weight=1;   

        #2.把请求分配到连接数最少的server
        #least_conn;

        #3.每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理
        #可以解决session的问题。如果server挂掉,能自动剔除。
        #ip_hash;

        #nginx默认轮询下面的服务实例
        server 127.0.0.1:9014 weight=1; 
        server 127.0.0.1:9015 weight=2; 
    } 

    server {
        listen       81;  #监听端口
        server_name  localhost;  #服务器名,默认localhost

        #默认字符编码,默认koi8-r,可改为utf-8
        #charset koi8-r;
        #charset utf-8;

        #本服务器实例日志路径
        #access_log  logs/host.access.log  main;

        #默认请求配置
        location / {
             root   html;   #web服务器根目录,用来放网站程序的目录(eg:/usr/share/nginx/html/cfsweb2)
             index  index.html index.htm; #默认首页
             proxy_pass http://apiservice;  #请求转向的url地址,反向代理则填写对应upstream后面的域名
        }

        #错误页面
        #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;
    #    }
    #}

}
配置详细说明

 

 2.4  重新加载

nginx.exe -s reload  //重新加载nginx
如果不成功执行 taskkill /f /t /im nginx.exe,然后在启动  start nginx.exe

 

 




posted on 2024-12-26 10:26  夕颜~~  阅读(58)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3