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)$" { # 使用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,可能和最初的值有不同,比如經過重定向之類的 $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,可能和最初的值有不同,比如經過重定向之類的