|
| 1 | +error_log logs/error.log debug; |
| 2 | + |
| 3 | +events { } |
| 4 | + |
| 5 | +http { |
| 6 | + proxy_cache_path cache/ keys_zone=auth_cache:10m; |
| 7 | + proxy_headers_hash_max_size 512; |
| 8 | + proxy_headers_hash_bucket_size 128; |
| 9 | + |
| 10 | + upstream auth-backend { |
| 11 | + server 127.0.0.1:8080; |
| 12 | + } |
| 13 | + |
| 14 | + server { |
| 15 | + listen 80; |
| 16 | + |
| 17 | + location / { |
| 18 | + auth_request /auth-proxy; |
| 19 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 20 | + proxy_set_header Host $http_host; |
| 21 | + |
| 22 | + # 认证后端使用了XSRF过滤,使用Nginx内部重定向会导致XSRF验证失效, |
| 23 | + # 因为,登录页面Get请求是由Nginx完成的,但Post请求是客户端完成的,这两个HTTP请求会分别分配给两个不同beego.Controller实例来处理, |
| 24 | + # 又因为XSRF Token保存在beego.Controller实例中随机生成的,所以Get和Post请求对应的XSRF Token不一样导致校验失败, |
| 25 | + # 因此,这里需要使用named location,在named location返回一个302响应,全部由客户端进行重定向和处理XSRF |
| 26 | + error_page 401 =200 @error401; |
| 27 | + } |
| 28 | + |
| 29 | + location @error401 { |
| 30 | + # 302跳转后,无法获取到之前的uri来设置X-Target头,因此,在302跳转的url加上target参数以达到相同效果 |
| 31 | + return 302 /passport/login?target=$request_uri; |
| 32 | + } |
| 33 | + |
| 34 | + location = /auth-proxy { |
| 35 | + internal; |
| 36 | + |
| 37 | + proxy_pass http://auth-backend/auth-proxy; |
| 38 | + proxy_pass_request_body off; |
| 39 | + proxy_set_header Content-Length ""; |
| 40 | + proxy_set_header X-CookieName "SessionID"; |
| 41 | + proxy_set_header Cookie SessionID=$cookie_SessionID; |
| 42 | + |
| 43 | + proxy_cache auth_cache; |
| 44 | + proxy_cache_valid 200 10m; |
| 45 | + proxy_cache_key "$http_authorization$cookie_SessionID"; |
| 46 | + } |
| 47 | + |
| 48 | + location /passport/login { |
| 49 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 50 | + proxy_set_header Host $http_host; |
| 51 | + # proxy_set_header X-Target $request_uri; # target通过url地址参数target来设置 |
| 52 | + proxy_pass http://auth-backend/passport/login; |
| 53 | + } |
| 54 | + |
| 55 | + location /passport/logout { |
| 56 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 57 | + proxy_set_header Host $http_host; |
| 58 | + proxy_pass http://auth-backend/passport/logout; |
| 59 | + } |
| 60 | + |
| 61 | + location /captcha { |
| 62 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 63 | + proxy_set_header Host $http_host; |
| 64 | + proxy_pass http://auth-backend/captcha; |
| 65 | + } |
| 66 | + |
| 67 | + location /favicon.ico { |
| 68 | + alias /var/www/site/assets/img/favicon.ico; |
| 69 | + proxy_set_header Host $host; |
| 70 | + proxy_set_header X-Real-IP $remote_addr; |
| 71 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 72 | + } |
| 73 | + |
| 74 | + location /static { |
| 75 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 76 | + proxy_set_header Host $http_host; |
| 77 | + proxy_pass http://auth-backend/static; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments