简体   繁体   中英

Nginx node setup to custom directory

I am using nginx first time so need help.

My app is running in /root/project1/tools (this directory is having server.js)

How i can connect nginx to this directory. I searched lot and do not find direct ans. Think nginx will find my server.js by port number not by path. is that true?

I am using linux ubuntu 18

More over nginx is throwing error

2018/10/23 06:14:51 [alert] 3822#3822: *2025 socket() failed (24: Too many open files) while connecting to upstream, client: 127.0.0.1, server: nativeiconba$

/etc/nginx/sites-available/nativeiconbase.com

 upstream app_yourdomain {
        server 127.0.0.1:8080;
        keepalive 8;
    }



# the nginx server instance
server {
    listen 80;
    listen [::]:80;
    server_name nativeiconbase.com www.nativeiconbase.com;
    access_log /var/log/nginx/nativeiconbase.com.log;

    # pass the request to the node.js server with the correct headers
    # and much more can be added, see nginx config options
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://nativeiconbase/;
      proxy_redirect off;
    }
 }


  root /root/project1/src/;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name localhost;

/etc/nginx/sites-available/default

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

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /root/project1/src/;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
        location / {
                # First attempt to serve request as file, then
                proxy_pass http://10.139.32.25: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;
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

And my node app is running on port 8080. Any idea what can i do to setup nginx. any reference to resource will be helpful.

All you have to do is setup a Reverse Proxy Server in Nginx

Start your NodeJS Server on whatever port

node server.js

If you are using any process management tool like pm2 then

pm2 server.js

Now in nginx config what you have to do is proxying all request to local nodejs server so

upstream app_yourdomain {
  server 127.0.0.1:8080;
  keepalive 8;
}



# the nginx server instance
server {
  listen 80;
  listen [::]:80;
  server_name nativeiconbase.com www.nativeiconbase.com;
  access_log /var/log/nginx/nativeiconbase.com.log;

  # pass the request to the node.js server with the correct headers
  # and much more can be added, see nginx config options
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://localhost:8080;
    proxy_redirect off;
  }
}

I have just changed the line proxy_pass http://localhost:8080 in your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM