1

So, I have tried to put a Nginx reverse proxy between my AngularJS client and the NodeJS server. All my static files are served as expected but some routes which are accessed using $http.get from an Angular service return 404. (such as app.get('/sessionInit') or app.get('/login')).

I will append to this some relevant parts of my code hoping that hey might help you in any way.

This is my API endpoint:

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 is my angular request:

    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;
            };

This is my nginx config file:

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;
    }
}

When trying to request the 'sessionInit' with angular from my NodeJS server where I have my route it is returning a publicip/sessionInit 404 not found error. Any help is appreciated. Thank you !

1 Answer 1

2

Put a / before 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
        }));
    }
});

In your Nginx config try_files $uri $uri/ =404; will also cause an issue. /sessionInit is not an existing file and therefore will redirect to 404, add a specific section for your api before your location / {.. section and call /api/sessionInit from your angular app:

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;
}

In server_name, don't put an ip but an hostname like: mywebsite.local then edit your hosts file (/etc/hosts on linux/max) and append 127.0.0.1 mywebsite.local

--

Not Question Related

Don't bother yourself with res.send(JSON.stringify(..)) and use res.json(..) which would more look like the following:

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
        });
    }
});

Be also carful with CORS and think about using expressjs/cors if necessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.