我正在使用nginx作为侦听端口80(http)的反向代理。 我正在使用proxy_pass转发请求到后端http和https服务器。 一切工作正常我的http服务器,但是当我尝试通过nginx反向代理到达https服务器的HTTPS服务器的IP显示在客户端的Web浏览器。 我希望显示nginx服务器的URI,而不是https后端服务器的ip(再一次,这可以正常使用http服务器,但不能用于https服务器)。 在论坛上看到这个post
这是我的configuration文件:
server { listen 80; server_name domain1.com; access_log off; root /var/www; if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 444; } location / { proxy_pass http://ipOfHttpServer:port/; } } server { listen 80; server_name domain2.com; access_log off; root /var/www; if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 444; } location / { proxy_pass http://ipOfHttpsServer:port/; proxy_set_header X_FORWARDED_PROTO https; #proxy_set_header Host $http_host; } }
当我尝试“proxy_set_header主机$ http_host”指令和“proxy_set_header主机$主机”的网页无法到达(页面未find)。 但是当我评论它,HTTPS服务器的IP地址显示在浏览器(这是坏的)。
有没有人有一个想法?
我的其他configuration文件是:
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; #proxy_hide_header X-Powered-By; proxy_intercept_errors on; proxy_buffering on; proxy_cache_key "$scheme://$host$request_uri"; proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m inactive=7d max_size=700m; user www-data; worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; server_names_hash_bucket_size 64; sendfile off; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; gzip on; gzip_comp_level 5; gzip_http_version 1.0; gzip_min_length 0; gzip_types text/plain text/html text/css image/x-icon application/x-javascript; gzip_vary on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
谢谢你的帮助 !
我遵循你的build议和你的例子,并将我的caching指令移动到位置块内的外部服务器块和代理指令。 我仍然有完全相同的问题:当proxy_set_header Host $host; 写的https网站是通过nginx不可缓解的。
当我发表评论时,尽pipeproxy_pass指令和proxy_redirectclosures,但我可以通过nginx访问https服务器,但是https服务器的lan ip地址显示在地址栏中。 但它仍然适用于http服务器(显示nginx的ip而不是http服务器ip)。
再精确一点:我一到http://addressOfMyNginx/ ,就没有到达https网页。 之前有一个警告页面,因为证书没有被validation。 在这个页面上,我仍然have http://addressOfMyNginx/地址栏中的have http://addressOfMyNginx/ 。 但是,当我按照“继续到网站无论如何”的链接,我被redirect到https网站,然后显示https服务器的IP地址。
阅读debugging日志后,我发现:
2012/07/30 17:24:13 [debug] 4412#0: *75 http proxy header: "GET / HTTP/1.0^M Host: nameOfMMyNginxServer^M X-Real-IP: xxx.xxx.xxx.xxx^M X-Forwarded-For: xxx.xxx.xxx.xxx^M Connection: close^M Accept: text/html, application/xhtml+xml, */*^M Accept-Language: fr-FR^M User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)^M Accept-Encoding: gzip, deflate^M Cookie: a_cookie_which_has_nothing_to_do_with_my_nginx_and_mybackend_server^M
其中xxx.xxx.xxx.xxx是一个服务器的公共地址,它与nginx或我的后端服务器无关(并且与之前提到的cookie没有任何关系)。
我重新加载/重新启动并清除了我的浏览器的caching和nginx的caching很多时间,因为我testing了可能涉及这个cookie的服务器。 但是xxx.xxx.xxx.xxx真的与这一切毫无关系。
我不能评论最后一篇文章,因为我发布了一个匿名帐户,我清除了我的浏览器的caching。 所以SF没有把我重组为Vulpo了…(然后我创build了一个帐户)。
proxy_redirect off应该做的伎俩。 我想你也应该改变你的proxy_pass来使用SSL,如果你想为你的后端使用SSL的话。 虽然Unix套接字将更好地加强安全性,并保持快速连接。
我推荐的nginx.conf:
# /etc/nginx/nginx.conf user www-data; worker_processes 2; # Do you really have two CPU cores? events { multi_accept on; worker_connections 768; use epoll; } http { charset utf-8; client_body_timeout 65; client_header_timeout 65; client_max_body_size 10m; default_type application/octet-stream; index index.html index.php /index.php; keepalive_timeout 20; reset_timedout_connection on; send_timeout 65; sendfile on; server_names_hash_bucket_size 64; tcp_nodelay off; tcp_nopush on; gzip on; gzip_buffers 32 4k; gzip_comp_level 2; gzip_disable "msie6"; gzip_http_version 1.1; gzip_min_length 1100; gzip_proxied any; gzip_static on; gzip_types #text/html is always compressed by HttpGzipModule text/css text/plain application/javascript application/x-javascript application/json application/x-json application/rss+xml application/xml application/vnd.ms-fontobject font/truetype font/opentype image/x-icon image/svg+xml; gzip_vary on; include mime.types; include conf.d/*.conf; include sites-enabled/*; }
我推荐的虚拟主机configuration:
# /etc/nginx/sites-available/default.conf proxy_cache_key "$scheme://$host$request_uri"; proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m inactive=7d max_size=700m; server { listen 80; server_name example.com; access_log off; root /var/www; # Consider using a map for this! If is bad! if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 444; } location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for; proxy_intercept_errors on; proxy_buffering on; proxy_pass http://127.0.0.1:port$request_uri; } }
看看我的nginxconfiguration在GitHub更先进的东西(还没有完成,必须先写更多的评论): https : //github.com/Fleshgrinder/nginx