Nginx反向代理讲解和配置

首先来介绍下Nginx的反向代理。代理服务器一般分为正向代理(通常直接称为代理服务器)和反向代理。

画个图我们就好理解了。

正向代理:可以想象成是路由器,我们要通过它来上网的那种。(可以说是客户端的代理)

反向代理:客户端的请求过来之后交给反向代理服务器,然后反向代理服务器再交给后台真实的服务器。(这个是服务器端的代理)

我们今天说的是nginx的反向代理功能的实现。同时,反向代理还可以实现负载均衡的功能。可以自己思考下。

由于实验比较简单,这边环境就简单处理。

http-server1:192.168.10.156(Apache服务)

http-server2:192.168.10.157(nginx服务)

nginx-proxy:192.168.10.159

保证http服务器搭建完成,并能正常访问

nginx-proxy安装(其他依赖条件自己安装)。版本:nginx-1.4.7.tar.gz

tar -zxvf nginx-1.4.7.tar.gz /home

./configure

--prefix=/usr/local/nginx \

--sbin-path=/usr/local/bin  \

--conf-path=/etc  \

--error-log-path=/usr/local/nginx/error.log  \

--pid-path=/usr/local/nginx/ngnix.pid \

--lock-path=/usr/local/nginx/nginx.lock  \

--with-http_ssl_module  \

--with-http_gzip_static_module  \

--with-http_perl_module \

--http-log-path=/usr/local/nginx/access.log \

--http-fastcgi-temp-path=/usr/local/nginx/html \

--with-pcre --with-zlib=/usr --with-openssl=/usr

make && make install

编辑nginx的配置文件。

vim /etc/nginx.conf

#user  nobody;

worker_processes  1;

events {

worker_connections  1024;

}

http {

upstream loadbance {

server 192.168.10.156;

server 192.168.10.157;(如果其其他端口,后面请加端口号:X.X.X.X:8888)

}

server {

listen 80;

location / {

proxy_pass http://loadbance;

}

}

}

红色部分为搭建反向代理服务器所需要的简单配置。(注意,nginx行尾的分号)

upstream:反向代理的关键字

loadbance:你可以理解为后台一组服务器的名称

server: 后台真实的服务器地址,可以是IP或者FQDN(如果是FQDN,别忘记解析)

listen:监听的端口,如果没有把原来nginx做web服务器的配置内容注销掉,建议改为其他端口

proxy_pass:后面跟http://loadbance  此一定要和upstream后面(服务器组名称)的名称保持一致

OK,大功告成。访问进行测试。

访问代理服务器地址:http://192.168.10.159,然后刷新

这就是nginx的反向代理。其实我们还可以延伸下。

nginx的反向代理功能我认为就可以当做一个简单的负载均衡来使用,我们可以指定nginx的调度算法:

nginx的官网上说(其实个人认为不止4中,你可以在服务器的ip地址后面加上服务器的权值,但是按官网上加上权值是不算一种。)

NGINX supports four load balancing methods:

1):The round-robin method    轮询

2):The least_conn method  最少连接数

a request is sent to the server with the least number of active connections with server weights taken into consideration。请求会送到活跃链接最少的服务器上(服务器的权值要考虑进来的)

3):The ip_hash method

The method guarantees that requests from the same address get to the same server unless it is not available。这种方法保证了来自同一个IP地址的请求会得到同一个服务器的响应,除非挂了。(其是通过客户端的ip(IPV4 or IPV6)地址来计算出此IP的hash值的)

4);The generic hash method:

the server to which a request is sent is determined from a user-defined key which may be a text, variable, or their combination. For example, the key may be a source IP and port,

请求会发送到一个用户定义键值(可以是text,变量,或者混合的)的服务器上

再来看一个官网说的:

If a method other than the default one is used, the corresponding directive (least_conn,ip_hash or hash) should be specified inside the upstream before the server directives.如果你要使用其他的调度算法(默认使用的round-robin),相应的指令必须在upstream内部指定,并且要在server指令之前。

比如:最小数链接/ip_hash/hash $request_uri consistent;(如果是使用轮询的方式的话,就不用写了,因为默认算法就是轮询)

upstream loadbance {

least_conn;   //或者是ip_hash和(hash $request_uri consistent)

server 192.168.10.156 weight=3;  //可以设置权值的哦

server 192.168.10.157;(如果其其他端口,后面请加端口号:X.X.X.X:8888)

}

哎呀,我擦,这就扯远了。其实,nginx还有一个健康检查的功能,如果有台服务器挂了,请求不会分发到该服务器上。比如下面的写法:

upstream loadbance {

server 192.168.10.156 weight=3;

server 192.168.10.157;

server 192.168.10.158 backup;//当备机使用

server 192.168.10.155 down;  //如果你要临时移除一台服务器进行升级或者其他操作,可以把其标记为down的状态,这样请求就不会到其上。

}

其实,我们还可以通过server内部的location指令来指定不同的路径分发到不同的服务器上去。来实现分流。

比如,可以定义多个upstream。upstream1,upstream2.......然后

location  /mp3 {

proxy_pass http://upstream1;

}

location /flv {

proxy_pass http://upstream2;

}

时间: 2024-08-09 09:22:34

Nginx反向代理讲解和配置的相关文章

nginx 反向代理apache服务器 配置java与PHP共存环境

listen 80; listen 443; ssl on; ssl_certificate /passport.crt; ssl_certificate_key /passport.key; ssl_session_timeout 5m; server_name localhost; index index.html index.htm index.php; root /www/; location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/ph

nginx反向代理原理和配置讲解

最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡.所以搜罗了一些关于反向代理服务器的内容,整理综合. 一  概述 反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器:并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器. 通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接

配置LANMP环境(7)-- 配置nginx反向代理,与配置apache虚拟主机

一.配置nginx反向代理 1.修改配置文件 vim /etc/nginx/nginx.conf 在35行http下添加一下内容: include /data/nginx/vhosts/*.conf; include /etc/nginx/upstream.conf; 2.在/etc/nginx/目录下新建 upstream.conf文件 vim upstream.conf upstream dev.test1.com { server 127.0.0.1(换成虚拟机ip):8080 weigh

Centos7.4 Nginx反向代理+负载均衡配置

Ningx是一款高性能的HTTP和反向代理服务器,配置起来也比较简单. 测试环境: 172.16.65.190 Nginx-反向代理 172.16.65.191 Ningx-Web 172.16.65.192 Nginx-Web 在三台Server安装Nginx: # yum install -y nginx 在172.16.65.190配置Nginx反向代理+负载均衡: # vim /etc/nginx/nginx.conf user nginx; worker_processes auto;

Nginx反向代理Tomcat的配置方法

一.Nginx安装 下载nginx源安装包 http://nginx.org/en/linux_packages.html 找到对应Linux版本的连接,这里选CentOS 6,右击复制链接地址"http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm " 在linux下下载nginx源的rpm包并安装 wget http://nginx.org/packages

nginx反向代理负载均衡配置

LB: nginx.conf worker_processes  1; events {     worker_connections  1024; } http {     include       mime.types;     default_type  application/octet-stream;     sendfile        on;     keepalive_timeout  65;     upstream www_server_pools {     serve

Nginx 反向代理的正确配置

server { listen 80; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; location /{ proxy_pass http://paila; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE

nginx反向代理负载均衡

1.反向代理概述 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器. 环境准备: 主机名 IP地址 角色 系统 web-node1.com eth0:192.168.90.201 web-node1节点 CentOS7.2 web-node2.com eth0:192.168.90.202 web-node

Nginx反向代理的配置

Chapter: Nginx基本操作释疑 1. Nginx的端口修改问题 2. Nginx 301重定向的配置 3. Windows下配置Nginx使之支持PHP 4. Linux下配置Nginx使之支持PHP 5. 以源码编译的方式安装PHP与php-fpm 6. Nginx多站点配置的一次实践 7. Nginx反向代理的配置 Nginx 作为 web 服务器一个重要的功能就是反向代理.其实我们在前面的一篇文章<Nginx多站点配置的一次实践>里,用的就是 Nginx 的反向代理,这里简单再