在上一篇:nginx 正向代理转发http跟https 中,我们的nginx还不能转发非80和443端口的url地址,虽说目前http的域名跟https的域名默认端口是80跟443,但总有一些特例嘛。这就需要nginx安装支持lua脚本的模块,方法自行google,不多累述。下面直接上nginx的配置文件。
#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 1024;
}
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;
server {
listen 12306;
charset utf-8;
# dns resolver used by forward proxying
resolver 114.114.114.114;
# forward proxy for CONNECT request
proxy_connect;
proxy_connect_allow all;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
# forward proxy for non-CONNECT request
location / {
set $passport 80;
if ($http_host ~ "(:(\d){4})$"){
set_by_lua $port "
local host = ngx.req.get_headers()[\"host\"];
local port = string.sub(host,-4);
return port ";
set $passport $port ;
}
if ($http_host ~ "(:(\d){3})$"){
set_by_lua $port "
local host = ngx.req.get_headers()[\"host\"];
local port = string.sub(host,-3);
return port ";
set $passport $port ;
}
if ($http_host ~ "(:(\d){2})$"){
set_by_lua $port "
local host = ngx.req.get_headers()[\"host\"];
local port = string.sub(host,-2);
return port ";
set $passport $port ;
}
if ($scheme = 'http') {
proxy_pass http://$host:$passport$request_uri;
}
if ($scheme = 'https') {
proxy_pass https://$host$request_uri;
}
proxy_set_header Host $host;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
}
}
}