Nginx代理非80端口

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

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,可能和最初的值有不同,比如经过重定向之类的


相关文章
|
4月前
|
应用服务中间件 网络安全 nginx
【Nginx】Nginx启动显示80端口占用问题的解决方案
【Nginx】Nginx启动显示80端口占用问题的解决方案
780 1
|
应用服务中间件 nginx Java
|
应用服务中间件 nginx
Nginx代理frp_web_80端口
官方issues中已经有相关问题的解答了:https://github.com/fatedier/frp/issues/53 参考:https://miss.
1247 0
|
24天前
|
应用服务中间件 nginx Docker
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
这篇文章介绍了如何通过域名在本地访问虚拟机上的nginx服务,包括创建nginx容器、修改配置文件、修改本地host文件以及进行访问测试的详细步骤。文章提供了具体的Docker命令来创建并配置nginx容器,展示了配置文件的修改示例,说明了如何在本地系统的hosts文件中添加虚拟机IP和自定义域名,以及如何通过浏览器进行测试访问。
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
|
4天前
|
应用服务中间件 nginx
一文搞定Nginx配置RTMP!
一文搞定Nginx配置RTMP!
32 3
|
5天前
|
Ubuntu 应用服务中间件 数据库
Nginx配置:阻止非国内IP地址访问的设置方法
此外,出于用户隐私和法律合规性的考虑,应慎重考虑阻止特定国家或地区IP地址的决策。在某些情况下,这可能被视为歧视性或违反当地法律。
15 2
|
20天前
|
Ubuntu 应用服务中间件 Linux
在Linux中,如何配置Web服务器(如Apache或Nginx)?
在Linux中,如何配置Web服务器(如Apache或Nginx)?
|
21天前
|
缓存 负载均衡 应用服务中间件
【揭秘】nginx代理配置全攻略:从零到精通,一文带你玩转高效网络代理的秘密武器!
【8月更文挑战第22天】nginx是一款高性能的HTTP与反向代理服务器,支持代理服务、负载均衡及缓存等功能,有助于提升网站响应速度和安全性。首先需确保已安装nginx,可通过包管理器进行安装。安装后启动并确认nginx运行状态。接着编辑配置文件(通常位于`/etc/nginx/nginx.conf`),设置代理转发规则,例如指定目标服务器地址和请求头信息。配置完成后测试有效性并重新加载nginx以应用更改。可以通过部署简易HTTP服务器验证代理功能是否正常工作。此外,还可以通过扩展配置文件实现更复杂的代理需求,如基于路径的代理和SSL加密等。
83 2