简体   繁体   English

Nginx节点设置到自定义目录

[英]Nginx node setup to custom directory

I am using nginx first time so need help. 我是第一次使用nginx,因此需要帮助。

My app is running in /root/project1/tools (this directory is having server.js) 我的应用程序在/ root / project1 / tools中运行 (此目录包含server.js)

How i can connect nginx to this directory. 我如何将Nginx连接到此目录。 I searched lot and do not find direct ans. 我搜索了很多,但没有找到直接的答案。 Think nginx will find my server.js by port number not by path. 认为nginx将通过端口号而不是路径找到我的server.js。 is that true? 真的吗?

I am using linux ubuntu 18 我正在使用linux ubuntu 18

More over nginx is throwing error Nginx引发错误

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$ 2018/10/23 06:14:51 [alert] 3822#3822:* 2025在连接到上游时socket()失败(24:打开文件太多),客户端:127.0.0.1,服务器:nativeiconba $

/etc/nginx/sites-available/nativeiconbase.com /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 在/ etc / nginx的/网站可用/默认

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. 我的节点应用程序正在端口8080上运行。任何想法我该怎么做才能设置nginx。 any reference to resource will be helpful. 对资源的任何引用都会有所帮助。

All you have to do is setup a Reverse Proxy Server in Nginx 您所需要做的就是在Nginx中设置反向代理服务器

Start your NodeJS Server on whatever port 在任何端口上启动NodeJS Server

node server.js

If you are using any process management tool like pm2 then 如果您使用的是诸如pm2类的任何流程管理工具,那么

pm2 server.js

Now in nginx config what you have to do is proxying all request to local nodejs server so 现在在nginx config中,您要做的就是将所有请求代理到本地nodejs服务器,这样

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 我刚刚在您的代码中更改了proxy_pass http://localhost:8080

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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