简体   繁体   English

Nginx配置与Node.js一起使用

[英]Nginx configuration to work with Node.js

I know there are similar questions or walkthroughs here or in web although i am stuck on this. 我知道在这里或在网络中也有类似的问题或演练,尽管我对此感到困惑。

The background 的背景

I am trying to set up a local virtual host ghost.local where there will be a ghost blog(which is on Node.js). 我试图建立一个本地虚拟主机ghost.local,那里将有一个幽灵博客(位于Node.js上)。 So i want, when i visit http://ghost.local on my Ubuntu 14.04 machine the Nginx would pass request to Node.js and i 'll be able to see my ghost blog. 所以我想,当我在Ubuntu 14.04机器上访问http://ghost.local时,Nginx会将请求传递给Node.js,这样我就可以看到我的幽灵博客。

The problem 问题

I 've set up hosts file i 've set up nginx vhosts my ghost is running on Node.js. 我已经设置了hosts文件,我已经设置了nginx vhosts,我的幽灵正在Node.js上运行。 Although when i visit http://ghost.local i get 虽然当我访问http://ghost.local时,我得到了

503 Service Unavailable Failed to resolve the name of server ghost.local 503服务不可用无法解析服务器ghost.local的名称

My hosts file: 我的主机文件:

 127.0.0.1  ghost.local
 127.0.0.1  localhost

My /etc/nginx/sites-available/ghost.local file: 我的/etc/nginx/sites-available/ghost.local文件:

 server {
     listen 80;
     server_name ghost.local;
     access_log /var/log/nginx/ghost.local.log;

     location / {        
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-for $remote_addr;
         proxy_set_header HOST $http_host; 

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

The link in /etc/nginx/sites-enabled/ghost.local is set up right. /etc/nginx/sites-enabled/ghost.local中的链接设置正确。

I didn't post anything from ghost or node.js because on http://localhost:2368 is up and running everything fine. 我没有发布来自ghost或node.js的任何内容,因为在http:// localhost:2368上已启动并且一切正常。

Any idea anyone? 有人知道吗? Thank you all in advance. 谢谢大家。

EDIT As i mentioned in my comment the problem most probably was on my machine set up the above configuration worked in a new machine. 编辑正如我在评论中提到的那样,问题很可能是在我的机器上设置了在新机器上工作的上述配置。

First off it seems like you installed the wrong version of nginx. 首先,您似乎安装了错误的nginx版本。 Did you install it yourself, if so you should reinstall nginx with the latest version. 您是否自行安装了它,如果是的话,应该重新安装最新版本的nginx。

Secondly, I am confused if your website is on http://ghost.local ? 其次,如果您的网站位于http://ghost.local上,我会感到困惑。

because server_name is supposed to be the sub/domain for your website, her is an example: 因为server_name应该是您网站的子/域,所以她是一个示例:

server_name www.example.com example.com test.example.com;

Here is what a basic nginx configuration for a website at http://example.com 这是位于http://example.com的网站的基本nginx配置

server {
        listen 80;
        server_name example.com;
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host      $http_host;
                proxy_pass                 http://127.0.0.1:2368;
        }
}

server {
        listen 80;
        server_name www.example.com;
        return 301 http://example.com/$request_uri;
}

Make sure you restart nginx afterwords. 确保重新启动Nginx后记。

service nginx restart

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

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