博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用nginx做反向代理
阅读量:3962 次
发布时间:2019-05-24

本文共 6216 字,大约阅读时间需要 20 分钟。

文章目录

原理

反向代理(reverse proxy):是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

在这里插入图片描述

nginx配置文件

user nobody;#启动进程,通常设置成和cpu的数量相等worker_processes  1; #全局错误日志及PID文件#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info; #pid        logs/nginx.pid; #工作模式及连接数上限events {
#epoll是多路复用IO(I/O Multiplexing)中的一种方式, #仅用于linux2.6以上内核,可以大大提高nginx的性能 use epoll; #单个后台worker process进程的最大并发链接数 worker_connections 1024; # 并发总数是 worker_processes 和 worker_connections 的乘积 # 即 max_clients = worker_processes * worker_connections # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么 # 为什么上面反向代理要除以4,应该说是一个经验值 # 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000 # worker_connections 值的设置跟物理内存大小有关 # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数 # 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右 # 我们来看看360M内存的VPS可以打开的文件句柄数是多少: # $ cat /proc/sys/fs/file-max # 输出 34336 # 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内 # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置 # 使得并发总数小于操作系统可以打开的最大文件数目 # 其实质也就是根据主机的物理CPU和内存进行配置 # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。 # ulimit -SHn 65535 } http {
#设定mime类型,类型由mime.type文件定义 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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件, #对于普通应用,必须设为 on, #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off, #以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; #tcp_nopush on; #连接超时时间 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #开启gzip压缩 gzip on; gzip_disable "MSIE [1-6]."; #设定请求缓冲 client_header_buffer_size 128k; large_client_header_buffers 4 128k; #设定虚拟主机配置 server {
#侦听80端口 listen 80; #定义使用 www.nginx.cn访问 server_name www.nginx.cn; #定义服务器的默认网站根目录位置 root html; #设定本虚拟主机的访问日志 access_log logs/nginx.access.log main; #默认请求 location / {
#定义首页索引文件的名称 index index.php index.html index.htm; } # 定义错误提示页面 error_page 500 502 503 504 /50x.html; location = /50x.html {
} #静态文件,nginx自己处理 location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#过期30天,静态文件不怎么更新,过期可以设大一点, #如果频繁更新,则可以设置得小一点。 expires 30d; } #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置. location ~ .php$ {
fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #禁止访问 .htxxx 文件 location ~ /.ht {
deny all; } }}

修改nginx的配置文件,实现负载均衡

nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器。

#服务器的集群  upstream tomcat_server_pool{
#服务器集群名字 server 101.200.166.221:8080 weight=1; #服务器配置 weight是权重的意思,权重越大,分配的概率越大。 server 101.200.166.221:8081 weight=2; } #当前的Nginx的配置 server {
listen 80; #监听80端口,可以改成其他端口 server_name 101.200.166.221; # 当前nginx服务的域名 location / {
proxy_pass http://tomcat_server_pool; index index.jsp index.html index.htm; } }

https配置

server {
listen 2443 ssl; server_name www.xxxxx.com:2443; ssl on; ssl_certificate cert/cert.crt; ssl_certificate_key cert/cert.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; proxy_redirect http:// $scheme://; port_in_redirect on; location ~ ^/yst-orp {
proxy_pass http://192.168.1.23:8081; proxy_set_header Host $host:$server_port; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }proxy_redirect参数;这个参数主要用来改从被代理服务器传来的应答头中的"Location"和"Refresh"字段。配置语法为:proxy_redirect [ default|off|redirect replacement ]默认的值为:proxy_redirect default 可使用的标签:http,server,location在此处配置为proxy_redirect http:// $scheme://;的作用是将从tomcat中返回的http修改为https。官方文档地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect port_in_redirect参数;这个参数的作用是启用或禁用在由nginx发布的绝对重定向中指定端口。配置语法:port_in_redirect [off|on]默认值:port_in_redirect on可使用此配置的标签:http,server,location此处配置为port_in_redirect on,作用是在nginx反向代理跳转到tomcat时将跳转到的端口替换为nginx监听的端口。官方文档地址:http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect proxy_set_header参数这个参数的作用是允许重新定义或者添加发往后端服务器的请求头。value可以包含文本、变量或者它们的组合。 当且仅当当前配置级别中没有定义proxy_set_header指令时,会从上面的级别继承配置。 默认情况下,只有两个请求头会被重新定义:proxy_set_header Host $proxy_host;proxy_set_header Connection close;配置语法格式:port_in_redirect field value;可使用的标签:http,server,location此处设置为proxy_set_header Host $host:$server_port;的作用是将请求的报文的头部的客户端的ip更改为当前作为反向代理的nginx的监听的ip及端口,这样后端的tomcat处理完请求时返回给nginx,再由nginx代理将结果返回给客户端。不然,后端服务处理完请求会直接返回到客户端,这样会导致客户端请求的https跳转为http,以及请求超时。官方文档地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header绑定域名,通过域名访问配置完成后,在本地通过nginx配置的https可以正常访问(配置了https,通过ip加端口的方式访问时提示安全性问题是正常的),但是通过对外提供的域名加端口不能正常访问。这是由于nginx配置中proxy_set_header参数的设置所引起的,proxy_set_header设置为Host $host:$server_port,这样导致将客户端请求报文头部的源地址修改为nginx本地监听地址加端口即:(192.168.1.25):2443,,在内外端口不一致时对外将请求结果返回给客户端时目标地址将变为:域名(www.xxxxx.com):2443,这样导致客户通过域名访问时输入的地址www.xxxxx.com:1234变为www.xxxxx.com:2443。解决这个问题只需更改配置(修改匹配值proxy_set_header Host $host:$server_port;)为:proxy_set_header Host www.xxxxx.com:1234;这样设置的作用就是,服务端处理完请求,将处理结果原路返回,最终由www.xxxxx.com:1243代理服务端发送给客户端。这样设置后本地直接通过nginx主机访问时也会跳转为域名地址。注意:在通过域名加端口的方式测试时,nginx对外映射的地址和自己测试的终端的出口地址不能为同一公网ip,否则。本地通过域名测试时无法访问。

转载地址:http://rqezi.baihongyu.com/

你可能感兴趣的文章
Ruby+Watir安装
查看>>
(原博客转移)诺基亚手机成板砖无法开机后,强刷修复手机系统的方法!本人亲测
查看>>
Ruby使用Win32API来操作鼠标
查看>>
代替Watir中click_no_wait的方法——left_click
查看>>
autoit3 ie.au3 函数之——_IE_Example、_IE_Introduction
查看>>
Android开发之——自定义标题栏titlebar
查看>>
autoit3 ie.au3 函数之——_IE_VersionInfo
查看>>
autoit3 ie.au3 函数之——_IEAction
查看>>
autoit3 ie.au3 函数之——_IEGetObjById、_IEGetObjByName
查看>>
autoit3 ie.au3 函数之——_IEAttach
查看>>
autoit3 ie.au3 函数之——_IEBodyReadHTML、_IEBodyWriteHTML
查看>>
autoit3 ie.au3 函数之——_IEBodyReadText
查看>>
autoit3 ie.au3 函数之——_IECreate
查看>>
autoit3 ie.au3 函数之——_IECreateEmbedded
查看>>
autoit3 ie.au3 函数之——_IEDocGetObj
查看>>
autoit3 ie.au3 函数之——_IEDocInsertHTML
查看>>
autoit3 ie.au3 函数之——_IEDocWriteHTML
查看>>
autoit3 ie.au3 函数之——_IEErrorHandlerDeRegister & _IEErrorHandlerRegister
查看>>
autoit3 ie.au3 函数之——_IEErrorNotify
查看>>
autoit3 ie.au3 函数之——_IEFormElementCheckBoxSelect & _IEFormGetObjByName
查看>>