简体   繁体   English

Nginx.conf和Node.js设置方案

[英]Nginx.conf and Node.js setup scenario

Today I've installed a NGINX server for a first time. 今天我第一次安装了NGINX服务器。 It works very well but I'm facing a small problem with the configuration of server to work together with node.js. 它工作得很好但我面临一个小问题,服务器的配置与node.js一起工作。

I want to have the following logic in the nginx.conf. 我想在nginx.conf中使用以下逻辑。

  1. Directory listing to be disabled 要禁用的目录列表
  2. All static files(images, js, less and css) to be served from the NGINX 从NGINX提供的所有静态文件(images,js,less和css)
  3. All requests like http://hostname/remote_data/??/??/ ?????? 所有请求如http:// hostname / remote_data / ?? / ?? / ?????? to be routed to the node.js server 被路由到node.js服务器
  4. All requests like http://hostname/??/??/ ?????? 所有请求如http:// hostname / ?? / ?? / ?????? to be routed to the index.html, so not to reach the node.js 要路由到index.html,所以不要到达node.js

question marks are optional parameters :) It is possible to have from 0 to 7 parameters. 问号是可选参数:)可以有0到7个参数。

I apologize if this setup scenario is very easy to be done, but I'm fighting with it almost 3 hours and I'm stuck. 如果这个设置方案很容易完成,我很抱歉,但我正在与它斗争近3个小时而且我被卡住了。 Step 1 and 2 are ready - 10x to google. 第1步和第2步准备就绪 - 谷歌10倍。

Regards Dan 关心丹

You should check out this answer . 你应该看看这个答案 From following the accepted answer there I got something like this: 从接受了接受的答案后我得到了这样的东西:

upstream node_app {
    server localhost:8080;
}

server {

  listen 80;
  server_name FOO_HOSTNAME_GOES_HERE;

  root /the/root/to/foo/document/root;
  access_log /var/log/nginx/foo.access.log;
  error_page 404 /404.html;

  location /remote_data/ {

    # Proxy request to node:

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://node_app;
    proxy_redirect off;

    access_log /var/log/nginx/foo_nodeapp.access.log;

  }

  location / {
    try_files $uri $uri/index.html 404;
  }

}

Untested though. 虽然未经测试。

I've managed to make it work with the following conf: 我已设法使其与以下conf配合使用:

server {
    root   /var/www;
    listen       80;
    server_name  _;


    location ~* /remote_data {
        # Proxy request to node:

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://node_app;
        proxy_redirect off;
        break;
    }       

    location / {
        index  index.html index.htm;

         location ~ \.(js|css|png|jpg|jpeg|gif|ico|html|less)$ {
             expires max;
             break;
        }

        rewrite ^/(.*)?$ /index.html?q=$1 last;
    }

    # serve static files directly
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
        access_log        off;
        expires           30d;
    }
}

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

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