使用express设置节点js的nginx

时间:2015-11-02 22:42:38

标签: javascript node.js nginx

尝试设置我的nginx服务器以将url传递给nodejs并让nodejs将json反馈发送回浏览器。我将此添加到我的nginx服务器,所以只有当url包含/ boba /它将它发送到节点服务器的任何内容时。

location ~ ^/boba {
    proxy_pass http://localhost:3005;
            proxy_http_version 1.1;
}

我的nodejs脚本就是这样编写的

 var express = require('express');
var app = express();

app.get('/boba/', function(req, res) {
    console.log("you did it!");
  res.json({
       thumbnail:  '.jpg'
  });
});
 console.log("END");
app.listen(process.env.PORT || 3005);

当我尝试在浏览器中加载文件时,我找不到文件。我不确定我是否需要将boba仍然包含在nodejs中的url中,或者只包含这样的/。或者如果它应该是别的东西。任何帮助

1 个答案:

答案 0 :(得分:0)

这对我有用:

http {
            proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
            proxy_temp_path /var/tmp;
            include       mime.types;
            default_type  application/octet-stream;
            sendfile        on;
            keepalive_timeout  65;

            gzip on;
            gzip_comp_level 6;
            gzip_vary on;
            gzip_min_length  1000;
            gzip_proxied any;
            gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
            gzip_buffers 16 8k;
    }

    upstream myserver {
            server 127.0.0.1:3000;
            keepalive 64;
    }



    server {
            listen 80;

            server_name http://www.myServer myServer;

            #error_page 502  /errors/502.html;

            location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|font/|robots.txt|humans.txt|favicon.ico) {
                    root /var/www/html/myServer/public;
                    access_log off;
                    expires max;
            }

            #location /errors {
                    #  internal;
                    #  alias /usr/local/silly_face_society/node/public/errors;
            #}

            location / {
                    proxy_redirect off;
            proxy_set_header   X-Real-IP            $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   Host                   $http_host;

    proxy_set_header   X-NginX-Proxy    true;
            proxy_set_header   Connection "";
            proxy_http_version 1.1;
            proxy_cache one;
            proxy_pass         myserver;
            }
    }

确保路由指向您的localhost路由。