B.D.
B.D.

Reputation: 31

nginx server routing issue (404)

I'm using nginx to host multiple node applications. I am working on linking localhost:80 to the forever applications I have running.

I have multiple applications that initially work like below where I just go to each port:

____________      ___________     
|           |    |    my     |
| localhost | -->| running   |
|           |    |  app(s)   |
|  (port #) |    |           |
|___________|    |___________|   

But since we have about 4 apps on different ports we want it to work like this:

____________      ___________     ____________
|           |    |           |   |    my      |
|  nginx    | -->| proxy     |-->|  running   |
|localhost80|    | pass      |   |   app(s)   |
|___________|    |___________|   |____________|

nginx should be routing to a page however I get "404 Not Found" which means the nginx is working but it has no idea what it's looking for.

Here is my code for one sample server application that I am trying to run:

Its a forever application (.js file) that is listening to a certain port.

#Server proxy for haiyan_dashboard
server {
      listen        80; #
      server_name     localhost/haiyan;

    location / {
      proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

}

Also, here is my entire config file:

user  nobody;
worker_processes  5;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
worker_connections  1024;
}


http {
include       mime.types;
default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
listen      80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
       #root   html;
         #root /var/www/webviz.redcross.org/public_html;
       proxy_pass https://webviz.redcross.org/services/tables;
       #index  index.html index.htm;
    }

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

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
#Server proxy for haiyan_dashboard
server {
      listen        8080; #
      server_name     localhosthaiyan;

    location / {
      proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

}
#Proxy for Haiti_Baseline
server {
    listen          3001;
    server_name     localhaiti_baseline;

    location / {
    proxy_pass http://localhost:3001/secure/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
}
    #ServerProxy for RFL Border
server {
    listen          80; #is this also 3002???
    server_name     rfl_border;

    location / {
    proxy_pass http://localhost:3002/secure/; #Change the URL!!!
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
}
    #ServerProxy for Basic Borders
server {
    listen          80;
    server_name     basic_border;

    location / {
    proxy_pass http://localhost:9001/secure/; #This URL is Different
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       80;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}

Am I using proxy pass properly? If not, how does it work in lamest terms. Additionally, ALL 4 pieces of server code sits in the nginx.conf file. Is this also a good practice.

(Note: I have already read the documentation on the nginx website)

If you need me to provide more clarification, I'll be happy to add it.

Upvotes: 0

Views: 3085

Answers (1)

B.D.
B.D.

Reputation: 31

Here is the solution:

server {
      listen      80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    # Default Route
    location / {
    root html;
    }

    # Route to haiyan
    location /haiyan_dashboard {
       #root   html;
         #root /var/www/webviz.redcross.org/public_html;
       #proxy_pass https://webviz.redcross.org/services/tables;
       proxy_pass http://localhost:8080/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host $host;
       proxy_cache_bypass $http_upgrade;
       #index  index.html index.htm;
    }

    # Route to Haiti Baseline
    location /Haiti_baseline {
    proxy_pass http://localhost:3001/secure/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

    # Route to RFL_Border
    location /RFL_border-master {
    proxy_pass http://localhost:3002/secure/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

    # Route to Basic Borders
    location /basic-borders {
    proxy_pass http://localhost:9001/secure/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }

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

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

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

`

Upvotes: 1

Related Questions