manzhe 2010-09-25
拟机上搭了 haproxy nginx tomcat的架构模型
没有haproxy的时候 nginx监听80端口 将动态请求转发给后端的8080
有haproxy后 haproxy监听80 转发给nginx的8001 nginx在讲动态的请求转发给tomcat的8080
但是就有了一个问题, 请求的url后面会包含有8001端口的
最后找到了解决办法
在nginx.conf
server { listen 8001; server_name beta.google.com; location / { proxy_pass http://tomcat; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
这样就不会有8001了
haproxy.conf
listen lbserver *:80 cookie JSESSIONID prefix # - equal weights on all servers # - maxconn will queue requests at HAProxy if limit is reached # - minconn dynamically scales the connection concurrency (bound my maxconn) depending on size of HAProxy queue # - check health every 20000 microseconds option httpchk GET /index server nginx1 127.0.0.1:8001 weight 1 minconn 3 maxconn 6 check inter 20000 server tomcat2 127.0.0.1:8082 weight 1 minconn 3 maxconn 6 check inter 20000
后来8001又出现了
因为配了一个
rewrite /mmm /fff permanent;
这样在转发后就又出现了8001
解决方法
rewrite /mmm http://beta.google.cn/fff permanent;
看现象的话 nginx内部转发的话 你是什么端口的来的就转发到什么端口 8001 来的 自然转发到8001
但从外部来的话 就是80 来的
网上找相关资料
Hi, everyone. I have a server running a php web forum. User logging into my site using 'logging.php'. I had set up a https server using nginx, but consdering server load, I just want my user using https in only logging.php. I want to settle this by using url rewrite. When my users click http://mysite.com/logging.php, nginx will automatic change the url to https://mysite.com/logging.php . And when my user click any other links in my site, for example, https://mysite.com/index.php, nginx will change https to http. Can rewrite work like this? Thank you! BTW,changing my forum codes may work, but I didn't know much about php coding...so, I want to settle this in nginx rewrite...
In your plain http server block: if ($uri ~* "/logging.php$") { rewrite ^/(.*)$ https://$host/$1 redirect; } In your https server block if ($uri !~* "/logging.php$") { rewrite ^/(.*)$ http://$host/$1 redirect; } This is when you are using standard ports (80 for HTTP and 443 for HTTPS). If you are using non-standard ports (say 8080 for http, and 8443 for https, then in this case, you should have in your http block) if ($uri ~* "/logging.php$") { rewrite ^/(.*)$ https://$host:8443/$1 redirect; } and correspondingly, in your https block, you should have: if ($uri !~* "/logging.php$") { rewrite ^/(.*)$ http://$host:8080/$1 redirect; } The $host variable is the host portion of the URL that was used to reach your server See http://wiki.codemongers.com/NginxHttpCoreModule for the list of variables