前提:因为本次需要两个软件在本机不同端口提供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. MySQL Disk--MySQL磁盘问题排查

    存储问题排查 1.检测磁盘空间使用率 df -lh 注意系统目录的可用空间 2.检查磁盘inode使用 df -i 参考:https://www.cnblogs.com/gaogao67/p/1088 ...

  2. 从零搭建consul

    从零搭建consul 原文链接:https://blog.csdn.net/weixin_42107541/article/details/87640807#2linux_25 从零搭建consul1 ...

  3. selenium常用的API(五)获取title、刷新、前进和后退

    获取网页title的属性值 #encoding=utf-8 from selenium import webdriver import unittest import time class Visit ...

  4. python通过LXML库读取xml命名空间

    xml实例版本: <a> <city:table xmlns:city="city"> <heilongjiang name="citys& ...

  5. Vue的参数请求与传递

    Vue不同模板之间的参数传递 页面路由带参数的跳转: 参数接收: Vue向服务器请求资源的两种方式 VUE-RESOURCE 1.Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不 ...

  6. [VSCode] Adding Custom Syntax Highlighting to a Theme in VSCode

    VSCode Themes are a quick way to update the color scheme and syntax highlighting of your code, but y ...

  7. mybatis自动生成model、dao及对应的mapper.xml文件

    背景: 日常开发中,如果新建表,手动敲写model.dao和对应的mapper.xml文件,费时费力且容易出错, 所以采用mybatis自动生成model.dao及对应的mapper.xml文件.代码 ...

  8. H5页面中判断是安卓手机还是ios手机的方法;APP页面中嵌套的H5跳转到APP其他页面的方法。

    (一).在H5页面中,可以直接利用如下的方法来进行判断是安卓还是ios. var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linu ...

  9. [THUPC2019]不等式/[51Nod1598]方程最小值

    [THUPC2019]不等式/[51Nod1598]方程最小值 题目大意: 给定\(a_{1\sim n}\)和\(b_{1\sim n}\),令\(f_k(x)=\sum_{i=1}^k|a_ix+ ...

  10. 51 NOD 1239 欧拉函数之和(杜教筛)

    1239 欧拉函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究 ...