Nginx做web服务器反向代理
实验目的
通过nginx实现反向代理的功能,类似apache反向代理和haproxy反向代理
有些公司从web服务器到反向代理,都使用nginx。nginx在1.9版本加入了tcp的反向代理功能
甚至安全策略:nginx+lua 完全可以搞定。
打开nginx官网

nginx做反向代理,安装命令如下,使用www用户运行nginx
useradd -s /sbin/noglogin -M www
wget http://nginx.org/download/nginx-1.9.12.tar.gz
tar zxf nginx-1.9.12.tar.gz
cd nginx-1.9.12
./configure --prefix=/usr/local/nginx-1.9.12 \
--user=www --group=www --with-http_ssl_module \
--with-http_stub_status_module --with-file-aio
make && make install
ln -s /usr/local/nginx-1.9.12/ /usr/local/nginx
检查语法
[root@linux-node2 nginx-1.9.12]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful
[root@linux-node2 nginx-1.9.12]#
检查服务器有无其它服务占用80端口,可以关闭了。
[root@linux-node1 ~]# /usr/local/httpd/bin/apachectl -k stop
配置nginx反向代理,修改主配置文件
gzip是默认关闭的
长连接默认打开的
sendfile 默认打开的
[root@linux-node1 conf]# cat 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 10240;
} 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 backend {
server 10.0.1.105:8080 weight=1 max_fails=3 fail_timeout=30s;
server 10.0.1.106:8080 weight=2 max_fails=3 fail_timeout=30s;
} server {
listen 80;
server_name www.nginx-nmap.com; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
proxy_pass http://backend;
} #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;
# }
#} }
[root@linux-node1 conf]#
负载均衡配置时的2个参数:fail_timeout和max_fails
这2个参数一起配合,来控制nginx怎样认为upstream中的某个server是失效的当在fail_timeout的时间内,某个server连接失败了max_fails次,则nginx会认为该server不工作了。
同时,在接下来的 fail_timeout时间内,nginx不再将请求分发给失效的server。
比如失败3次,那么接下来10秒不会之内不会把请求发个这个认为失败的机器。然后过了30秒后,这个机器继续收到探测请求.一般生产中设置为30秒
upstream backend {
server 10.0.1.105:8080 weight=1 max_fails=3 fail_timeout=30s;
server 10.0.1.106:8080 weight=2 max_fails=3 fail_timeout=30s;
}
关于nginx反向代理功能由下面模块提供


检测语法,启动或者reload。查看监听状态
[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful
[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@linux-node1 conf]# netstat -lntp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27141/nginx: master
tcp6 0 0 :::8080 :::* LISTEN 20130/httpd
[root@linux-node1 conf]#
浏览器测试


[root@linux-node2 nginx-1.9.12]# systemctl stop httpd
[root@linux-node2 nginx-1.9.12]# systemctl start httpd
[root@linux-node2 nginx-1.9.12]#
关于会话保持


重启
[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful
[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@linux-node1 conf]#

关于nginx的负载均衡算法有很多,自行百度
Nginx做web服务器反向代理的更多相关文章
- Nginx是什么,有什么优点?为什么选择Nginx做web服务器软件?(经典经典)
1.基础知识 代理服务器: 一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器,代理服务器一般作用在客户端.应用比如:GoAgent,FQ神器. 一个完整的代理请求过程为:客 ...
- 解决Vue用Nginx做web服务器报错favicon.ico 404 (Not Found)的问题
有多种解决方案 1.vue静态资源 vue中为网页增加favicon的最便捷的方式为使用link标签 <link rel="shortcut icon" type=" ...
- 通过Apache配置web服务器反向代理
- 第一步: 到安装好的apache文件目录conf文件下,找到httpd.conf文件 找到如下配置,去掉#可以启动HTTP反向代理功能 : LoadModule proxy_module modu ...
- 使用Nginx实现服务器反向代理和负载均衡
前言 同事总问我Nginx做反向代理负载均衡的问题,因此特意留下一篇扫盲贴! 直接部署服务器的风险 假设,我开发了一个网站,然后买了一台Web服务器和一台数据库服务器,直接部署到公共网络上.如下图,网 ...
- nginx高性能WEB服务器系列之七--nginx反向代理
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- 高性能Nginx服务器-反向代理
Nginx Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供 ...
- 死磕nginx系列--nginx服务器做web服务器
nginx 做静态服务器 HTML页面如下 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- 树莓派做web服务器(nginx、Apache)
一想到Linux Web服务器,我们首先想到的是: Apache + MySql + Php. Apache:是世界使用排名第一的Web服务器软件. 可以运行在几乎所有广泛使用的计算机平台上,由于其跨 ...
- 通过Nginx+tomcat+redis实现反向代理 、负载均衡及session同步
一直对于负载均衡比较陌生,今天尝试着去了解了一下,并做了一个小的实验,对于这个概念有一些认识,在此做一个简单的总结 什么是负载均衡 负载均衡,英文 名称为Load Balance,指由多台服务器以对称 ...
随机推荐
- html代码换行造成空格间距问题
连续几个内联标签或表单元素标签的换行在浏览器会被解释为一个空格. 比如下面代码: <span style="border:1px solid #f20">hello&l ...
- [flask]gunicorn配置文件
配置文件 #!/home/xx/.virtualenvs/xx/bin/python # encoding: utf-8 import multiprocessing # 监听端口 bind = '0 ...
- Angular4学习笔记(四)- 依赖注入
概念 依赖注入是一种设计思想,并不是某一类语言所特有的,因此可以参考开涛大神关于学习Java语言的Spring框架时对其的解释: DI-Dependency Injection,即"依赖注入 ...
- ABBYY Cup 3.0G3. Good Substrings
题意:定义一个串合法,在n个串中出现次数在li到ri中.问s的所有本质的子串有是多少合法的 题解:把所有串用分隔符分开建sam,记录一个该节点对应每个串的出现次数,topo排序后,当该节点s出现次数不 ...
- mysql,Jdbc工具类,只需一条sql实现简单查询
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import ...
- MongoDB 教程(八):查询文档、条件操作符
MongoDB 查询文档 MongoDB 查询文档使用 find() 方法. find() 方法以非结构化的方式来显示所有文档. MongoDB 查询数据的语法格式如下: db.collection. ...
- 指导手册05:MapReduce编程入门
指导手册05:MapReduce编程入门 Part 1:使用Eclipse创建MapReduce工程 操作系统: Centos 6.8, hadoop 2.6.4 情景描述: 因为Hadoop本身 ...
- 操作系统の实验四 windows中线程的创建和同步控制
摘要: 1.创建信号量 HANDLE CreateSemaphore( LPSECURITY_ATTIBUTES lpSemaphoreAttributes, LONG lInitialCount; ...
- 《TypeScript 中文入门教程》
转载:<TypeScript 中文入门教程> 17.注解 (2015-12-03 11:36) 转载:<TypeScript 中文入门教程> 16.Symbols (2015- ...
- ionic3 自定义组件 滑动选择器 ion-multi-picker
1.ionic3中有一个 ion-datatime 给大家选择时间提供了一个很方便的组件 效果如图 链接 https://ionicframework.com/docs/api/component ...