-1

I may need a little help here. I feel I am almost there but missed something in my setup.

I can connect to my .com/3000 to see my local node app running on server. To connect to .com directly I just need to use proxy_pass so I did

proxy_pass http://127.0.0.1:3000;

But it won't run.

I have a nodejs app running on localhost:3000 on server.

Below is my default file in /etc/nginx/sites-available

server {
    listen 80 default_server;
    listen [::]:80 default_server;


    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
            proxy_pass http://127.0.0.1:3000;
    }

 }

And here's my nginx.config file

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
include /etc/nginx/sites-enabled/*;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

include             /etc/nginx/mime.types;
default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
index   index.html index.htm;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

     # redirect server error pages to the static page /40x.html
    #
    error_page 404 /404.html;
        location = /40x.html {
    }

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

}

Update -

I am on Amazon Ec2 instance. The page at .com displays as

enter image description here

For root directive I have tried

  1. root /var/www/app/index.html and in this case it will just throw me 404 not found nginx page**
  2. root /var/www/app and in this case it gives 403 forbidden error**
6
  • What happens when you connect to port 80? What's your error message? Also note that you have default_server in both server blocks. Most probably the wrong one wins. Jul 26, 2018 at 13:03
  • are you getting correct data with .com:3000 directly. or are you appending app name like .com:300/app1 to access it.
    – Sunil Bhoi
    Jul 26, 2018 at 13:07
  • @SunilBhoi 3000 runs directly and works fine
    – HalfWebDev
    Jul 26, 2018 at 13:25
  • @GeraldSchneider I think it hits the /usr/share/nginx/html and gives me nginx welcome page. I tried to change this root to something else and it is now giving me 404 nginx page. For the default_server line in both files of server block, please go ahead and mention the corrected version. I am not even sure how default and nginx.conf are playing together. Just sort it out for me
    – HalfWebDev
    Jul 26, 2018 at 13:29
  • @kushalvm are you getting default nginx page with .com or is there any error. If its default page then try to add proxy_pass line in default server block
    – Sunil Bhoi
    Jul 26, 2018 at 13:44

2 Answers 2

1

Remove default_server from your nginx.config:

    listen       80; # default_server;
    listen       [::]:80; # default_server;

Then your default config file should get the requests. Also, I would only use server_name _; for a document root that should be used as a last resort when a request arrives for a domain that is not configured on your server. Use the actual domain name instead of _.

2
  • should I remove default_server from .conf file or default as well? coz the second change you mentioned is in default file
    – HalfWebDev
    Jul 26, 2018 at 14:04
  • I have tried commenting first in nginx.conf and then in default file as well. Doesn't work. Replaced server_name value with actual .com and still the same
    – HalfWebDev
    Jul 26, 2018 at 14:08
0

That is quite a confusing configuration file layout. I see a couple of things you should try:

Remove the server block from nginx configuration.

Make sure your default configuration file is saved in /etc/nginx/conf.d with a filename ending with .conf. This way it will be included in nginx configuration.

The current nginx configuration does not include any files in sites-available or sites-enabled directories, therefore those files have no effect.

One final tip, you should use nginx -T (note the capital T) to check what is the actual configuration nginx uses when all includes are processed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .