Nginx代理非80端口

Nginx通过80端口反向代理到Tomcat实现很简单,通过Jsp的request.getServerPort()获取到的端口号依然是80,而如果Nginx使用非80端口做反响代理时request.getServerPort()获取到的端口号返回依然会是80,这样边无法正确response.sendRedirect

String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

这样response.sendRedirect会跳转到80端口去

解决办法:

修改proxy.conf获取你把代理的配置写到了nginx内,也可以在其内部修改

proxy_redirect          off;

# nginx非80端口处理 
proxy_set_header        Host $host:$server_port; 
# 获取真实IP 
proxy_set_header        X-Real-IP $remote_addr; 
# 获取代理者的真实ip 
proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; 
client_max_body_size    10m; 
client_body_buffer_size 128k; 
proxy_connect_timeout   90; 
proxy_send_timeout      90; 
proxy_read_timeout      90; 
proxy_buffer_size       4k; 
proxy_buffers           4 32k; 
proxy_busy_buffers_size 64k; 
proxy_temp_file_write_size 64k;

nginx vhost内虚拟主机代理配置

location ~* "\.(jsp|do)$" { 
        proxy_pass http://kinggoo.ts;

# 使用include引入就可以了 
        include proxy.conf; 
}

参考帮助

Nginx内置变量含义

名称    版本    说明(变量列表来源于文件 ngx_http_variables ) 
$args    1.0.8    请求中的参数; 
$binary_remote_addr    1.0.8    远程地址的二进制表示 
$body_bytes_sent    1.0.8    已发送的消息体字节数 
$content_length    1.0.8    HTTP请求信息里的"Content-Length"; 
$content_type    1.0.8    请求信息里的"Content-Type"; 
$document_root    1.0.8    针对当前请求的根路径设置值; 
$document_uri    1.0.8    与$uri相同; 
$host    1.0.8    请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名; 
$hostname    1.0.8     
$http_cookie    1.0.8    cookie 信息 
$http_post    1.0.8     
$http_referer    1.0.8    引用地址 
$http_user_agent    1.0.8    客户端代理信息 
$http_via    1.0.8     最后一个访问服务器的Ip地址。 
$http_x_forwarded_for    1.0.8     相当于网络访问路径。 
$is_args    1.0.8     
$limit_rate    1.0.8    对连接速率的限制; 
$nginx_version    1.0.8     
$pid    1.0.8     
$query_string    1.0.8    与$args相同; 
$realpath_root    1.0.8     
$remote_addr    1.0.8    客户端地址; 
$remote_port    1.0.8    客户端端口号; 
$remote_user    1.0.8    客户端用户名,认证用; 
$request    1.0.8    用户请求 
$request_body    1.0.8     
$request_body_file    1.0.8    发往后端的本地文件名称 
$request_completion    1.0.8     
$request_filename    1.0.8    当前请求的文件路径名 
$request_method    1.0.8    请求的方法,比如"GET"、"POST"等; 
$request_uri    1.0.8    请求的URI,带参数; 
$scheme    1.0.8     所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect; 
$sent_http_cache_control    1.0.8     
$sent_http_connection    1.0.8     
$sent_http_content_length    1.0.8     
$sent_http_content_type    1.0.8     
$sent_http_keep_alive    1.0.8     
$sent_http_last_modified    1.0.8     
$sent_http_location    1.0.8     
$sent_http_transfer_encoding    1.0.8     
$server_addr    1.0.8    服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); 
$server_name    1.0.8    请求到达的服务器名; 
$server_port    1.0.8    请求到达的服务器端口号; 
$server_protocol    1.0.8    请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; 
$uri    1.0.8    请求的URI,可能和最初的值有不同,比如经过重定向之类的 
名称    版本    说明(变量列表来源于文件 ngx_http_variables ) 
$args    1.0.8    请求中的参数; 
$binary_remote_addr    1.0.8    远程地址的二进制表示 
$body_bytes_sent    1.0.8    已发送的消息体字节数 
$content_length    1.0.8    HTTP请求信息里的"Content-Length"; 
$content_type    1.0.8    请求信息里的"Content-Type"; 
$document_root    1.0.8    针对当前请求的根路径设置值; 
$document_uri    1.0.8    与$uri相同; 
$host    1.0.8    请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名; 
$hostname    1.0.8     
$http_cookie    1.0.8    cookie 信息 
$http_post    1.0.8     
$http_referer    1.0.8    引用地址 
$http_user_agent    1.0.8    客户端代理信息 
$http_via    1.0.8     最后一个访问服务器的Ip地址。 
$http_x_forwarded_for    1.0.8     相当于网络访问路径。 
$is_args    1.0.8     
$limit_rate    1.0.8    对连接速率的限制; 
$nginx_version    1.0.8     
$pid    1.0.8     
$query_string    1.0.8    与$args相同; 
$realpath_root    1.0.8     
$remote_addr    1.0.8    客户端地址; 
$remote_port    1.0.8    客户端端口号; 
$remote_user    1.0.8    客户端用户名,认证用; 
$request    1.0.8    用户请求 
$request_body    1.0.8     
$request_body_file    1.0.8    发往后端的本地文件名称 
$request_completion    1.0.8     
$request_filename    1.0.8    当前请求的文件路径名 
$request_method    1.0.8    请求的方法,比如"GET"、"POST"等; 
$request_uri    1.0.8    请求的URI,带参数; 
$scheme    1.0.8     所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect; 
$sent_http_cache_control    1.0.8     
$sent_http_connection    1.0.8     
$sent_http_content_length    1.0.8     
$sent_http_content_type    1.0.8     
$sent_http_keep_alive    1.0.8     
$sent_http_last_modified    1.0.8     
$sent_http_location    1.0.8     
$sent_http_transfer_encoding    1.0.8     
$server_addr    1.0.8    服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); 
$server_name    1.0.8    请求到达的服务器名; 
$server_port    1.0.8    请求到达的服务器端口号; 
$server_protocol    1.0.8    请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; 
$uri    1.0.8    请求的URI,可能和最初的值有不同,比如经过重定向之类的 
名称    版本    说明(变量列表来源于文件 ngx_http_variables ) 
$args    1.0.8    请求中的参数; 
$binary_remote_addr    1.0.8    远程地址的二进制表示 
$body_bytes_sent    1.0.8    已发送的消息体字节数 
$content_length    1.0.8    HTTP请求信息里的"Content-Length"; 
$content_type    1.0.8    请求信息里的"Content-Type"; 
$document_root    1.0.8    针对当前请求的根路径设置值; 
$document_uri    1.0.8    与$uri相同; 
$host    1.0.8    请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名; 
$hostname    1.0.8     
$http_cookie    1.0.8    cookie 信息 
$http_post    1.0.8     
$http_referer    1.0.8    引用地址 
$http_user_agent    1.0.8    客户端代理信息 
$http_via    1.0.8     最后一个访问服务器的Ip地址。 
$http_x_forwarded_for    1.0.8     相当于网络访问路径。 
$is_args    1.0.8     
$limit_rate    1.0.8    对连接速率的限制; 
$nginx_version    1.0.8     
$pid    1.0.8     
$query_string    1.0.8    与$args相同; 
$realpath_root    1.0.8     
$remote_addr    1.0.8    客户端地址; 
$remote_port    1.0.8    客户端端口号; 
$remote_user    1.0.8    客户端用户名,认证用; 
$request    1.0.8    用户请求 
$request_body    1.0.8     
$request_body_file    1.0.8    发往后端的本地文件名称 
$request_completion    1.0.8     
$request_filename    1.0.8    当前请求的文件路径名 
$request_method    1.0.8    请求的方法,比如"GET"、"POST"等; 
$request_uri    1.0.8    请求的URI,带参数; 
$scheme    1.0.8     所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect; 
$sent_http_cache_control    1.0.8     
$sent_http_connection    1.0.8     
$sent_http_content_length    1.0.8     
$sent_http_content_type    1.0.8     
$sent_http_keep_alive    1.0.8     
$sent_http_last_modified    1.0.8     
$sent_http_location    1.0.8     
$sent_http_transfer_encoding    1.0.8     
$server_addr    1.0.8    服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); 
$server_name    1.0.8    请求到达的服务器名; 
$server_port    1.0.8    请求到达的服务器端口号; 
$server_protocol    1.0.8    请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; 
$uri    1.0.8    请求的URI,可能和最初的值有不同,比如经过重定向之类的 名称 版本 说明(变量列表来源于文件 ngx_http_variables ) 
$args 1.0.8 请求中的参数; 
$binary_remote_addr 1.0.8 远程地址的二进制表示 
$body_bytes_sent 1.0.8 已发送的消息体字节数 
$content_length 1.0.8 HTTP请求信息里的"Content-Length"; 
$content_type 1.0.8 请求信息里的"Content-Type"; 
$document_root 1.0.8 针对当前请求的根路径设置值; 
$document_uri 1.0.8 与$uri相同; 
$host 1.0.8 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名; 
$hostname 1.0.8  
$http_cookie 1.0.8 cookie 信息 
$http_post 1.0.8  
$http_referer 1.0.8 引用地址 
$http_user_agent 1.0.8 客户端代理信息 
$http_via 1.0.8 最后一个访问服务器的Ip地址。 
$http_x_forwarded_for 1.0.8 相当于网络访问路径。 
$is_args 1.0.8  
$limit_rate 1.0.8 对连接速率的限制; 
$nginx_version 1.0.8  
$pid 1.0.8  
$query_string 1.0.8 与$args相同; 
$realpath_root 1.0.8  
$remote_addr 1.0.8 客户端地址; 
$remote_port 1.0.8 客户端端口号; 
$remote_user 1.0.8 客户端用户名,认证用; 
$request 1.0.8 用户请求 
$request_body 1.0.8  
$request_body_file 1.0.8 发往后端的本地文件名称 
$request_completion 1.0.8  
$request_filename 1.0.8 当前请求的文件路径名 
$request_method 1.0.8 请求的方法,比如"GET"、"POST"等; 
$request_uri 1.0.8 请求的URI,带参数; 
$scheme 1.0.8 所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect; 
$sent_http_cache_control 1.0.8  
$sent_http_connection 1.0.8  
$sent_http_content_length 1.0.8  
$sent_http_content_type 1.0.8  
$sent_http_keep_alive 1.0.8  
$sent_http_last_modified 1.0.8  
$sent_http_location 1.0.8  
$sent_http_transfer_encoding 1.0.8  
$server_addr 1.0.8 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); 
$server_name 1.0.8 请求到达的服务器名; 
$server_port 1.0.8 请求到达的服务器端口号; 
$server_protocol 1.0.8 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; 
$uri 1.0.8 请求的URI,可能和最初的值有不同,比如经过重定向之类的

时间: 2024-08-11 00:18:03

Nginx代理非80端口的相关文章

nginx做非80端口转发

nginx可以很方便的配置成反向代理服务器   server {listen 80;server_name localhost; location / {    proxy_pass http://147.16.24.175:9500 ;    proxy_set_header Host $host:80;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_fo

Nginx服务器中配置非80端口的端口转发方法详解_nginx

nginx可以很方便的配置成反向代理服务器: server { listen 80; server_name localhost; location / { proxy_pass http://x.x.x.x:9500; proxy_set_header Host $host:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy

服务器-求大神解惑,tomcat只能绑定域名到非80端口,用80端口绑定的话,外网无法通过域名访问

问题描述 求大神解惑,tomcat只能绑定域名到非80端口,用80端口绑定的话,外网无法通过域名访问 我按照网上的教程,已经把tomcat的端口号从8080改为了80,且配置好了host,如下所示(确认80端口未被占用,域名也已经备案,防火墙也关了): <Host name="www.xxx.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlVal

jboss非80端口多域名访问java应用

问题描述 现有一台主机服务器,用IIS部署了一个用.NET做的网站,80端口已经被占用.现有一个用ssh做的java网站,同样在该主机上用jboss-5.1.0.GA部署问题:已经申请了3个域名,如http://www.hello.com,http://hello.com,http://helloworld.com已经绑定到主机服务器的IP上,请问如何设置,就能通过在url地址栏中输入上述的不带端口号的三个网址就能访问我部署到jboss-5.1.0.GA的java应用?请大家帮忙,最好能附上配置

详解Nginx与Apache共用80端口的配置方法_nginx

一个典型的 Nginx + Apache 应用方案可以是Nginx 占用 80 端口,过滤静态请求,然后动态请求即 Proxy 到 Apache 的 8080 端口.Proxy 反向代理的好处是访问的时候,始终就是 80 端口,来访者不会觉察到有任何的区别. 但有的应用确非常"聪明",识别到 Apache 所位于的端口是 8080 ,就会把相关的超链接都一并加上 :8080 的后续.这么就死定了,还能有正常访问麽?! 有个方法可以解决这事,就是把 apache 也运行在80端口上.同一

利用Nginx反向代理功能解决WEB网站80端口被封的解决方法_nginx

大陆的网络环境,都在天朝神兽的制度下让我等小P民悲剧一片:动不动就拔网线.封机房:现在更厉害的一招,从网关封杀你的80端口,一旦被封,网站域名就无法访问: 以上便废话,下面转入正题,其它就不帖了,直接给代码,利用nginx的反向代理功能,一般大家都很看懂,也不多记录了: [code] server { listen 80; server_name a.jb51.net; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOT

Nginx和Apache共用80端口的配置教程

一个典型的 Nginx + Apache 应用方案可以是Nginx 占用 80 端口,过滤静态请求,然后动态请求即 Proxy 到 Apache 的 8080 端口.Proxy 反向代理的好处是访问的时候,始终就是 80 端口,来访者不会觉察到有任何的区别. 但有的应用确非常"聪明",识别到 Apache 所位于的端口是 8080 ,就会把相关的超链接都一并加上 :8080 的后续.这么就死定了,还能有正常访问麽?! 有个方法可以解决这事,就是把 apache 也运行在80端口上.同一

WAS6非root用户不能绑定80端口

问题描述 WAS6非root用户不能绑定80端口,提示80端口被占用,有哪位知道怎么解决的? 解决方案 解决方案二:可以查询一下那个程序正在使用80端口.如果是aix可以这样netstat-Aan|grep80查出sockaddrrmsocksockaddrtcpcb查出进程号.一般was和ihs(ibmhttp服务器)一起使用,是不是ihs占用了这个端口

IIS与Apache共用80端口方法

apache|iis 为调试程序,本机安装iis与apache,无法同时使用80端口,现给出解决方法:    方法一:     IIS5,多IP下共存,IIS为192.168.0.1,apache为192.168.0.2 原文地址    c:\Inetpub\Adminscripts    cscript adsutil.vbs set w3svc/disablesocketpooling true    该命令反馈如下disablesocketpooling : (BOOLEAN) True