單點登陸nginx代理及開發環境調試配置

最近作項目,遇到單點登陸,方法是在前端作nginx代理。
應用系統須要配合作以下調整:
一、使用「*.頂級域名」訪問,確保能夠和KAM共享cookie
二、對來自Nginx代理的訪問放開登陸認證(安全起見,對來自其餘IP的訪問應該禁止)
三、應用系統從請求頭kam_remote_user中讀取登陸用戶(Nginx會將登陸用戶寫入請求頭)
四、若是應用系統須要KAM的全局惟一token,能夠從cookie中讀取kam_sso_token的值html

剛開始調試的時候是在正式環境,每次修改完代碼須要從新構建才能看到效果,
實在繁瑣,還產生了一堆沒必要要的commit 歷史。調通以後開始思考如何在本地開發環境調試單點登陸這一功能。
不辱使命,研究出來了,如下是步驟。前端

一、配置項目 nginx.conf

location / {
            root   /usr/share/nginx/html;
            auth_request /kam_auth;
            error_page 401 = @error401;
            auth_request_set $kam_remote_url $upstream_http_kam_remote_url;
            proxy_set_header kam_remote_user $upstream_http_kam_remote_user;
        }

    location /kam_auth {
            internal;
            proxy_set_header kam_remote_url "$scheme://$http_host$request_uri";
            proxy_set_header Host $host;
            proxy_pass_request_body off;
            proxy_set_header Content-Length "";
            proxy_pass 單點登陸頁面網址/auth;
        }

    location @error401 {
            add_header Set-Cookie "NSREDIRECT=$kam_remote_url;Path=/;Domain=頂級域名";
            return 302 單點登陸頁面網址;
        }

配置完後發佈到正式環境,單點登陸功能已可用webpack

繼續配置,咱們的目標是 dev 環境也能單點登陸!nginx

二、虛擬域名

修改hosts文件 C:WindowsSystem32driversetc
加入 127.0.0.1 dev.testgroup.comweb

clipboard.png

以後,把原來的dev運行地址 http://localhost:8080 換成 http://dev.testgroup.com:8080
發現報錯 Invalid Host header
解決方法:在webpack.dev.conf.js devServer對象中添加:disableHostCheck: truejson

devServer: {
    ...
    disableHostCheck: true
  },

從新 run dev 後發現能夠訪問了瀏覽器

三、下載 nginx

nginx官網上下載相應的安裝包
下載進行解壓,將解壓後的文件放到本身心儀的目錄下。window的cmd窗口,進入到nginx目錄,
使用 start nginx.exe 進行nginx的安裝,以下圖所示安全

clipboard.png

安裝成功,後在瀏覽器地址欄輸入:127.0.0.1,會看到以下圖所示的nginx歡迎界面cookie

clipboard.png

若是看不到,那麼說明你安裝失敗,你能夠到你的 nginx 目錄下的logs文件夾下的error下查看,
若是發現裏面寫着:ui

clipboard.png

說明你的80端口被佔用了,或是cmd命令進入dos下執行:netstat -aon | findstr :80 查看80端口是否被佔用,若是佔用,那麼你須要修改註冊表,以下步驟:
一、打開註冊表:regedit
二、找到:HKEY_LOCAL_MACHINESYSTEMCurrentControlSetservicesHTTP
三、找到一個REG_DWORD類型的項Start,將其改成0
四、重啓系統,System進程不會佔用80端口
此時此刻,你能夠再次執行 相應的命令:start nginx.exe 命令了。

nginx相關命令:
start nginx.exe
nginx.exe -s stop //中止nginx
nginx.exe -s reload //從新加載nginx
nginx.exe -s quit //退出nginx

由於咱們已經配置了虛擬域名,因此在瀏覽器地址欄輸入 http://dev.testgroup.com 也是一樣的 nginx welcome 頁面

三、配置本地 nginx 文件

修改 nginx 目錄下的 conf/nginx.conf 文件

location / {
            root   html;
            index  index.html index.htm;
            auth_request /kam_auth;
            error_page 401 = @error401;
            auth_request_set $kam_remote_url $upstream_http_kam_remote_url;
            proxy_set_header kam_remote_user $upstream_http_kam_remote_user;
            proxy_pass http://dev.testgroup.com:8080;
        }
        location /kam_auth {
            internal;
            proxy_set_header kam_remote_url "$scheme://$http_host$request_uri";
            proxy_set_header Host $host;
            proxy_pass_request_body off;
            proxy_set_header Content-Length "";
            proxy_pass 單點登陸頁面網址/auth;
        }

         location @error401 {
            add_header Set-Cookie "NSREDIRECT=$kam_remote_url;Path=/;Domain=頂級域名";
            return 302 單點登陸頁面網址;
        }

如今,在瀏覽器地址欄輸入 http://dev.testgroup.com 發現自動跳轉到了單點登陸頁面,登陸後返回到了咱們以前的dev.testgroup.com頁面

相關文章
相關標籤/搜索