2016-06-08 21 views
1

因此,我嘗試在我的AngularJS客戶端和NodeJS服務器之間放置一個Nginx反向代理。我的所有靜態文件都按預期提供,但是使用來自Angular服務的$ http.get訪問的某些路由返回404(例如app.get('/ sessionInit')或app.get('/ login'))。AngularJS,Nodejs和Nginx應用程序在嘗試點擊終點時返回404

我會追加到我的代碼希望嘿可能以任何方式幫助你的一些相關部分。

這是我的API端點:

app.get('sessionInit', function(req, res){ 
    if(sess){ 
     res.send(JSON.stringify({ 
      code : 610, 
      scope : "session", 
      message : "Session on the way !", 
      session : sess 
     })); 
    }else { 
     res.send(JSON.stringify({ 
      code : 613, 
      scope : "session", 
      message : "Session does not exists or has expired please log in again.", 
      session : null 
     })); 
    } 
}); 

這是我的角度要求:

this.getSession = function getSession(){ 
       var promise = $http({ 
        url: '/sessionInit', 
        method: "GET" 
       }).then(function (result) { 
        if(result.data.code == 610){ 
         //update browser cookies 
         $cookies.putObject("userSession", result.data.session); 
         setUserInfo(); 
         if(typeof($cookies.get("userSession")) != null) return $cookies.get("userSession"); 
         else return null; 
        }else{ 
         console.log(result.data.code, result.data.message, result.data.session); 
         return null; 
        } 
       }); 

       return promise; 
      }; 

這是我nginx的配置文件:

server { 
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on; 

    root /usr/share/nginx/html/law-bid-nodejs; 
    index index.php index.html index.htm; 

    # Make site accessible from http://localhost/ 
    server_name *mypublicip*; 

    location/{ 
     try_files $uri $uri/ =404; 
     proxy_set_header 'Access-Control-Allow-Origin' '$http_origin'; 
     proxy_set_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE'; 
     proxy_set_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin'; 
     proxy_redirect off;   
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $remote_addr; 
     proxy_set_header Host $http_host; 
     proxy_pass   http://127.0.0.1:3000; 
    } 

    error_page 404 /404.html; 

    # redirect server error pages to the static page /50x.html 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
     root /usr/share/nginx/html; 
    } 

    location ~ \.php$ { 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    # # With php5-fpm: 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 
    } 

    # deny access to .htaccess files, if Apache's document root 
    # concurs with nginx's one 
    location ~ /\.ht { 
     deny all; 
    } 
} 

當試圖從我的NodeJS服務器請求帶有角度的'sessionInit'時,我有我的路由,它將返回一個publicip/sessionInit 404 not found錯誤。 任何幫助表示讚賞。謝謝 !

回答

2

將一個/以前sessionInit

app.get('/sessionInit', function(req, res){ 
    if(sess){ 
     res.send(JSON.stringify({ 
      code : 610, 
      scope : "session", 
      message : "Session on the way !", 
      session : sess 
     })); 
    }else { 
     res.send(JSON.stringify({ 
      code : 613, 
      scope : "session", 
      message : "Session does not exists or has expired please log in again.", 
      session : null 
     })); 
    } 
}); 

在你的Nginx配置try_files $uri $uri/ =404;也會引起問題。 /sessionInit不是現有的文件,因此將重定向到404,你location/{..部分之前添加特定部分爲您的API,並從您的角度程式呼叫/api/sessionInit

location /api/ { 
    proxy_pass http://nodejs/; 
    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_set_header X-Forwarded-Proto https; 
    proxy_connect_timeout 30; 
    proxy_send_timeout 300; 
    proxy_read_timeout 900; 
    send_timeout 30; 
} 

server_name,不要把一個ip而是像主機名:mywebsite.local然後編輯您的hosts文件(/etc/hosts在Linux /最大)和追加127.0.0.1 mywebsite.local

-

不會質疑相關

不要res.send(JSON.stringify(..))打擾自己,用res.json(..)這將更加如下所示:

app.get('/sessionInit', function(req, res){ 
    if(sess){ 
     res.json({ 
      code : 610, 
      scope : "session", 
      message : "Session on the way !", 
      session : sess 
     }); 
    }else { 
     res.json({ 
      code : 613, 
      scope : "session", 
      message : "Session does not exists or has expired please log in again.", 
      session : null 
     }); 
    } 
}); 

也carful與CORS,想想如果需要使用expressjs/cors

相關問題