Nginx节点设置到自定义目录

时间:2018-10-23 06:27:38

标签: node.js nginx

我是第一次使用nginx,所以需要帮助。

我的应用程序正在 / root / project1 / tools 中运行(此目录包含server.js)

我如何将Nginx连接到此目录。我搜索了很多,但没有找到直接的答案。认为nginx将通过端口号而不是路径找到我的server.js。是真的吗?

我正在使用linux ubuntu 18

更多关于nginx引发错误

  

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

我的节点应用程序正在端口8080上运行。任何想法我该怎么做才能设置nginx。任何对资源的引用都会有所帮助。

1 个答案:

答案 0 :(得分:0)

您要做的就是在Nginx中设置一个反向代理服务器

在任何端口上启动NodeJS服务器

node server.js

如果您使用的是pm2之类的任何流程管理工具,那么

pm2 server.js

现在在nginx配置中,您要做的就是将所有请求都代理到本地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;
  }
}

我刚刚在您的代码中更改了行proxy_pass http://localhost:8080