【发布时间】:2016-06-08 16:07:30
【问题描述】:
所以,我尝试在我的 AngularJS 客户端和 NodeJS 服务器之间放置一个 Nginx 反向代理。我的所有静态文件都按预期提供,但某些使用 $http.get 从 Angular 服务访问的路由返回 404。(例如 app.get('/sessionInit') 或 app.get('/login'))。
我将在此附加我的代码的一些相关部分,希望他们能以任何方式帮助你。
这是我的 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 服务器请求带有 angular 的“sessionInit”时,它返回了一个 publicip/sessionInit 404 not found 错误。 任何帮助表示赞赏。谢谢!
【问题讨论】: