nginx proxy_pass配置及关于setcookie的一点说明

作者:zhanhailiang 日期:2015-02-01

nginx proxy_pass配置透传hd域的动态请求到wapi域下:

server { listen 80; server_name hd.coalaawork.com; access_log logs/hd.coalaawork.com.access.log; root /usr/local/wwwroot/coalaawork/hd; location / { index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~* ^.+/.(js|css|gif|png|jpg|bmp|jpeg|htm|xml|swf|json|ico)$ { expires off; break; } location /cgi/ { proxy_pass http://wapi.coalaawork.com/; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name wapi.coalaawork.com; access_log logs/wapi.coalaawork.com.access.log; root /usr/local/wwwroot/coalaawork/wapi; location / { index index.html index.htm index.php; } if (!-f $request_filename) { rewrite ^/(.*)$ /index.php?s=$1 last; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~* ^.+/.(js|css|gif|png|jpg|bmp|jpeg|htm|xml|swf|json|ico)$ { expires off; break; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 

说明:

  1. 以上配置将匹配hd域下的/cgi/请求,转发到wapi域。注意proxy_pass http://wapi.coalaawork.com/最后的request_uri为/,详细测试用例请见nginx proxy_pass末尾神奇的/

  2. 转到时需要带上客户端的IP地址,需要配置:

    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
  3. 使用nginx -t发现“host not found in upstream "wapi.coalaawork.com"”,需要在/etc/hosts添加“127.0.0.1 wapi.coalaawork.com”:

    [root@/usr/local/nginx/conf/vhosts]# /usr/local/nginx/sbin/nginx -t
    nginx: [emerg] host not found in upstream "wapi.coalaawork.com" in /usr/local/nginx/conf/vhosts/coalaawork.com:24
    configuration file /usr/local/tengine-2.0.3/conf/nginx.conf test failed

测试如下:

[root@/usr/local/wwwroot/coalaawork]# find .
.
./wapi
./wapi/index.php
./hd
[root@/usr/local/wwwroot/coalaawork]# cat ./wapi/index.php 
<?php

echo json_encode($_SERVER);

setcookie设置$domain参数的三种区别

测试脚本如下:

[root@/usr/local/wwwroot/coalaawork/wapi]# cat index.php 
<?php

setcookie('test1', 1, 0);
setcookie('test2', 2, 0, 'wapi.coalaawork.com');
setcookie('test3', 3, 0, 'hd.coalaawork.com');
echo json_encode($_SERVER);

测试结果如下:

测试结果表明使用setcookie设置cookie时:
1. 默认不设置domain参数时,Set-Cookie时浏览器会在当前域下种下cookie;
2. 当设置domain参数时,Set-Cookie时浏览器在所有的domain子域名下都会带上该cookie(请见php setcookie函数domain参数说明);
3. Set-Cookie不允许跨域;

你可能感兴趣的:(nginx proxy_pass配置及关于setcookie的一点说明)