Nginx简介

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。由于Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新浪、网易、腾讯、搜狐等企业的一些门户网站等,在3w以上的高并发环境下,ngnix处理能力相当于apache的10倍。

Nginx负载均衡

分摊到多个操作单元上进行执行,保证所有后端服务器都将性能充分发挥,从而保持服务器集群的整体性能最优。

一、下载ngxin

下载地址 http://nginx.org/download/nginx-1.2.5.zip

解压后 双击nginx.exe 启动

查看进程出现 nginx.exe

浏览器输入http://localhost/

出现如下字样:

Welcome to nginx!

nginx启动成功

配置完环境变量后,就可以使用以下命令来启动和关闭Nginx服务。

1)start nginx 启动命令

2)nginx -s stop 快速停止nginx,并不保存相关信息.

3)nginx -s quit 完整有序的停止nginx,并保存相关信息。

4)nginx -t 测试

二、在三台服务器上分别建一个网站:

Server1 : 192.168.10.152:8887

Server2: 192.168.10.63:8887

Server3 : 192.168.10.134:8887

每个网站下放一个Index.html文件,内容为相应ip+端口

三、修改配置文件conf

找到 \conf\nginx.conf

打卡nginx.conf

配置如下

#user  nobody;
#nginx进程数,建议设置为等于CPU总核心数
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 8192;
} rtmp {
server {
listen 1935;
chunk_size 4000; application myapp {
live on;
} application zbcs {
live on;
record off;
} application vod {
play video;
} application hls {
live on;
hls on;
hls_path temp/hls;
hls_fragment 8s;
}
}
} #设定http服务器,利用他的反向代理功能提供负载均衡支持
http {
#设定mime类型
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;
log_format main '$remote_addr - $remote_user [$time_local]'
'"$request" $status $bytes_sent'
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"'
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local]'
'"$request" $status $bytes_sent'
'"$http_referer" "$http_user_agent"'
'"$http_range" "$sent_http_content_range"'; #设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k; #设定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m; sendfile on;
tcp_nopush on;
tcp_nodelay on; #keepalive_timeout 0;
keepalive_timeout 65; #开启gzip模块
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain application/x-javascript text/css application/xml; output_buffers 1 32k;
postpone_output 1460; server_names_hash_bucket_size 128;
client_max_body_size 8m; fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_vary on; #服务器集群名称 localhost
upstream localhost{
#根据ip计算将请求分配各那个后端tomcat,可以解决session问题,保持一个客户端多次请求分发到一台后端服务器上
ip_hash;
#weight权重,max_fails请求失败的次数 fail_timeout请求失败后暂停请求此服务器的时间
server 192.168.10.63:8887 weight=3 max_fails=1 fail_timeout=600s;
server 192.168.10.134:8887 weight=3 max_fails=1 fail_timeout=600s;
server 192.168.10.152:8887 weight=3 max_fails=1 fail_timeout=600s;
} server {
#监听端口
listen 8090;
server_name localhost; #charset koi8-r;
charset 'utf-8'; #access_log logs/host.access.log main; #对 "/" 启用负载均衡
location / {

        # 项目根目录
        root html;
        # 默认首页
        index index.html index.htm;

            #反向代理
proxy_pass http://localhost;
#设置主机头和客户端真实地址,以便服务器获取客户端真实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;
#允许客户端请求的最大单个文件字节数
client_max_body_size 10m;
#缓冲区代理缓冲用户端请求的最大字节数,可以理解为先保存到本地再传给用户
client_body_buffer_size 128k;
#跟后端服务器连接超时时间 发起握手等候响应超时时间
proxy_connect_timeout 2s;
#连接成功后 等待后端服务器响应时间 其实已进入后端的排队之中等候处理
proxy_read_timeout 90;
#后端服务器数据回传时间 就是在规定时间内后端服务器必须传完所有数据
proxy_send_timeout 90;
#代理请求缓存区 这个缓存区间会保存用户的头信息一共Nginx进行规则处理 一般只要能保存下头信息即可
proxy_buffer_size 4k;
#同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
proxy_buffers 4 32k;
#如果系统很忙的时候可以申请国内各大的proxy_buffers 官方推荐 *2
proxy_busy_buffers_size 64k;
#proxy 缓存临时文件的大小
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m; } #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;
# }
#} }

四、重启nginx

nginx -s quit 停止nginx

start nginx  启动nginx

五、发起请求

打开浏览器,访问http://localhost:8090

快速刷新页面,轮询出现三个服务器ip+端口

如果停止其中一台服务,再次快速刷新页面,不会出现停止服务的ip+端口

成功!

反向代理

其实上面已经出现了反向代理的配置及备注信息

如要访问http://192.168.10.134:8090/test/Index.html

第一种

编辑\conf\nginx.conf 里的server 段

location /test/ {
proxy_pass http://192.168.10.134:8887;
}

当访问http://localhost:8090/test/ 时nginx 会代理访问到 http://192.168.10.134:8887/test/Index.html

第二种

location /test/ {
proxy_pass http://192.168.10.134:8887/;
}

当访问http://localhost:8090/test/ 时nginx 会代理就会访问到 http://192.168.10.134:8887/Index.html

第三种

location /test/ {
proxy_pass http://192.168.10.134:8887/file/;
}

当访问http://localhost:8090/test/ 时nginx 会代理就会访问到 http://192.168.10.134:8887/file/Index.html

第四种

location /test/ {
proxy_pass http://192.168.10.134:8887/file;
}

当访问http://localhost:8090/test/ 时nginx 会代理就会访问到 http://192.168.10.134:8887/fileIndex.html

nginx 负载均衡及反向代理的更多相关文章

  1. Nginx 负载均衡和反向代理实践

    nginx 以哪个配置文件启动 Nginx 负载均衡和反向代理实践 环境介绍 192.168.1.50    在这台主机上配置Nginx 的反向代理,负载均衡,和web1,web1使用的81号端口 1 ...

  2. 【架构师之路】Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》

    本篇摘自<亿级流量网站架构核心技术>第二章 Nginx负载均衡与反向代理 部分内容. 当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用 ...

  3. CentOS中实现Nginx负载均衡和反向代理

    一.安装必要软件 负载均衡服务器:IP设置为192.168.1.10 Web服务器1:安装Apache或者Nginx,IP设置为192.168.1.11: Web服务器2:安装Apache或者Ngin ...

  4. nginx负载均衡和反向代理有什么区别

    近在研究nginx的负载均衡和反向代理,先看下这两个简单的配置吧! 负载均衡 worker_processes 1; events { worker_connections 1024; } http{ ...

  5. Nginx负载均衡和反向代理设置

    Nginx负载均衡: 格式: upstream 别名 {    #别名一般要有意义,能看出是做什么的 server ip:端口;    #要实现负载的服务器的ip.端口号}  例: upstream ...

  6. Nginx负载均衡和反向代理的配置和优化

    负载均衡 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位, 反向代理 是指以代理服务器来接受internet上的请求,然后将请求转给内部的服务器 常见的负载均衡 1. ...

  7. CentOS中nginx负载均衡和反向代理的搭建

    1: 修改centos命令行启动(减少内存占用): vim /etc/inittab :initdefault: --> 修改5为3 若要界面启动使用 startx 2:安装jdk )解压:jd ...

  8. Nginx负载均衡与反向代理—《亿级流量网站架构核心技术》

    当我们的应用单实例不能支撑用户请求时,此时就需要扩容,从一台服务器扩容到两台.几十台.几百台.然而,用户访问时是通过如http://www.XX.com的方式访问,在请求时,浏览器首先会查询DNS服务 ...

  9. Nginx负载均衡与反向代理的配置实例

    user www www; worker_processes 10; error_log /data1/logs/nginx_error.log crit; pid /usr/local/webser ...

随机推荐

  1. div盒子或者图片并排居中

    要使div总是找不到原因居中很简单,float和display都可以实现,float就不说了,这里说一下display:line-block,比如四个或者多个div盒子,明明设置好了宽度后,总有一个上 ...

  2. 微信小程序下拉刷新真机无法弹回

    在下拉函数里加上这句wx.stopPullDownRefresh(); /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { v ...

  3. 原生js实现计时器

    https://www.cnblogs.com/sandraryan/ 点击开始计时,可以计次,暂停.点了暂停可以继续计时,计次,点击重置清空. <!DOCTYPE html> <h ...

  4. 机器学习-RBF高斯核函数处理

     机器学习-RBF高斯核函数处理 SVM高斯核函数-RBF优化 重要了解数学的部分: 协方差矩阵,高斯核函数公式. 个人建议具体的求法还是看下面的核心代码吧,更好理解,反正就我个人而言,烦躁的公式,还 ...

  5. Spark1.6.1 MLlib 特征抽取和变换

    Spark1.6.1 MLlib 特征抽取和变换 1 TF-IDF TF-IDF是一种特征向量化方法,这种方法多用于文本挖掘,通过算法可以反应出词在语料库中某个文档中的重要性.文档中词记为t,文档记为 ...

  6. HDU 1568

    - - 我自己开始以为是数值范围是1到100000000.... 搞了半天才发现是斐波那契数列的项数1到100000000 坑爹.!! 不会,只能看网上大牛的题解. 具体解释请看:http://www ...

  7. [转]VsCode搭建Java开发环境(Spring Boot项目创建、运行、调试)

    源码地址:https://github.com/YANGKANG01/Spring-Boot-Demo 安装扩展 安装如下两个主要扩展即可,这两个扩展已关联java项目开发主要使用的maven.spr ...

  8. P1101 走迷宫一

    题目描述 大魔王抓住了爱丽丝,将她丢进了一口枯井中,并堵住了井口. 爱丽丝在井底发现了一张地图,他发现他现在身处一个迷宫当中,从地图中可以发现,迷宫是一个N*M的矩形,爱丽丝身处迷宫的左上角,唯一的出 ...

  9. P1000 A+B Problem

    题目描述 给定两个整数\(a,b\),输出它们的和. 输入格式 输入两个整数,表示\(a,b(1 \le a,b \le 10^9)\). 输出格式 输出一个整数,表示答案. 样例输入 20 30 样 ...

  10. 2018-2-13-git-合并两个仓库

    title author date CreateTime categories git 合并两个仓库 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 ...