前提:因为本次需要两个软件在本机不同端口提供http服务,于是我准备一个tomcat在8080端口,另一个nodejs程序在3000端口,前者自不用提,后者可以到https://www.cnblogs.com/xiandedanteng/p/7514174.html 去下载并安装配置Node.js启动。

nginx主要通过proxy_pass来配置反向代理,upstream模块实现负载均衡。我们简单修改C:\nginx-1.16.1\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  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream firstdemo{
        server 127.0.0.1:8080;
        server 127.0.0.1:3000;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

}

上面粗红体表示代理到firstdemo上。

粗蓝体则表示由8080和3000的端口分别对localhost:80的请求提供http服务。

之后使用C:\nginx-1.16.1>nginx.exe -s reload 重启nginx服务,在浏览器里输入localhost,再不断刷新,就可以轮换看到以下画面了:

这样每次刷新就会访问不同的服务器,至此我们实现了一个简单的负载均衡。

只启动一个比如3000的node server,不断刷新浏览器,然后再启动一个比如8080的tomcat,效果是什么样的诸位看官可以自行尝试一下。

--END-- 2019年12月14日06:37:45

Nginx配置简单负载均衡的更多相关文章

  1. Nginx配置之负载均衡、限流、缓存、黑名单和灰度发布

    一.Nginx安装(基于CentOS 6.5) 1.yum命令安装 yum install nginx –y(若不能安装,执行命令yum install epel-release) 2. 启动.停止和 ...

  2. Nginx配置及负载均衡

    转载:http://www.cnblogs.com/jingmoxukong/p/5945200.html nginx简易教程   目录 Nginx  概述  安装与使用  nginx 配置实战  参 ...

  3. nginx配置优化+负载均衡+动静分离详解

    nginx配置如下: #指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes 8;#全局错误日志文件#debug输出日志最为详细 ...

  4. nginx 配置https 负载均衡

    1.Winodw安装openssl工具(生成SSL证书用的)免编译版本下载: http://slproweb.com/products/Win32OpenSSL.html 注意:如果openssl在使 ...

  5. [转] nginx配置优化+负载均衡+动静分离(附带参数解析)

    #指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes  8;#全局错误日志文件#debug输出日志最为详细,而crit输出日志 ...

  6. nginx配置+uwsgi+负载均衡配置

    nginx静态文件配置 location /static{ alias /var/www/myApp/static; } sudo mkdir -vp /var/www/myApp/static/ s ...

  7. nginx配置实现负载均衡

    一.负载均衡的作用 1.转发功能 按照一定的算法[权重.轮询],将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量. 2.故障移除 通过心跳检测的方式,判断应用服务器当前是否可以正 ...

  8. Nginx配置实例-负载均衡实例:平均访问多台服务器

    场景 Nginx配置实例-反向代理实例:根据访问的路径跳转到不同端口的服务中: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  9. nginx 配置实例-负载均衡

    1.实现效果 (1)浏览器地址栏输入地址 http://www.123.com/edu/a.html,负载均衡效果,平均 8080 和 8081 端口中 2.准备工作 (1)准备两台 tomcat 服 ...

随机推荐

  1. QuickStart系列:docker部署之Gitlab本地代码仓库

    gitlab是可以在本地搭建的使用git作为源代码管理的仓库. 运行环境: win10+vmware14+docker7+docker 1. 使用命令拉取镜像(非必须,耗时比较久,这里以ce为准,ce ...

  2. 【Spring Boot】Spring Boot之跨域解决方案

    一.什么是跨域 跨域,指的是从一个域名去请求另外一个域名的资源.即跨域名请求!跨域时,浏览器不能执行其他域名网站的脚本,是由浏览器的同源策略造成的,是浏览器施加的安全限制. 跨域的严格一点来讲就是只要 ...

  3. ELK Stack部署

    部署ELK Stack 官网:https://www.elastic.co 环境准备: ip hostname 服务 用户.组 192.168.20.3 node2003 kibana6.5,file ...

  4. 安装python工具

    安装python编写工具 本篇幅只限于安装linux系统上. 想要实现linux程序在windows桌面上打开,需要在linux上打开X11forward和在windows上安装Xmanager ID ...

  5. 2013.6.29 - OpenNER第九天

    上午看计算机网络,下午做计算机实验.晚上写计算机实验报告,还有OpenStack的实验报告. 写完之后跟师兄讨论了一下OpenNER的事情,觉得OpenNE很像是化学物质,里面很多都可以构成原子团,原 ...

  6. CentOS7怎样安装Redis4.0.12

    一.安装 yum -y install tcl安装相关依赖 mkdir /usr/local/redis创建redis安装目录 cd /usr/local/redis 进入redis目录 wget h ...

  7. 两个不同的账户登录两个浏览器,导入同样的excel表格。数据出现重复

    1.场景: 两个不同的账户登录两个浏览器,导入同样的excel表格.业务逻辑已经有验重校验,但数据仍然出现重复,锁定是并发问题导致. 2.参考博客: https://cloud.tencent.com ...

  8. expect工具实现脚本的自动交互

    1 安装expect工具 expect是建立在tcl基础上的一个自动化交互套件, 在一些需要交互输入指令的场景下, 可通过脚本设置自动进行交互通信. 其交互流程是: spawn启动指定进程 -> ...

  9. iptables 通用语句

    iptables -t filter -nvL --line-number | grep destination -t :  指定表 {fillter|nat|mangle|raw} -v : 显示详 ...

  10. What is the syntax for a for loop in TSQL?

    loop  报错 英 [luːp]  美 [lup]  口语练习 vi. 打环:翻筋斗 n. 环:圈:弯曲部分:翻筋斗 vt. 使成环:以环连结:使翻筋斗 syntax  报错 英 ['sɪntæks ...