Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Nginx绑定端口上的Node.js应用程序_Javascript_Node.js_Ssl_Nginx_Nginx Reverse Proxy - Fatal编程技术网

Javascript 使用Nginx绑定端口上的Node.js应用程序

Javascript 使用Nginx绑定端口上的Node.js应用程序,javascript,node.js,ssl,nginx,nginx-reverse-proxy,Javascript,Node.js,Ssl,Nginx,Nginx Reverse Proxy,我试图在Nginx Ubuntu 17.04上使用SSL在端口上为生产设置Node.js应用程序。到目前为止,我已经启动并运行了sslnginx服务器 这是我的Nginx配置文件的外观: server { listen 80 default_server; listen [::]:80 default_server; listen 443 ssl; root /var/www/html; index index.php index.html index.htm index.nginx-debi

我试图在Nginx Ubuntu 17.04上使用SSL在端口上为生产设置Node.js应用程序。到目前为止,我已经启动并运行了
sslnginx
服务器

这是我的Nginx配置文件的外观:

server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;

root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;

server_name example.com www.example.com;

location / {
    try_files $uri $uri/ =404;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
    deny all;
}
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

if ($scheme != "https") {
    return 301 https://$host$request_uri;
} 

    ssl_dhparam /etc/ssl/certs/dhparam.pem;

}
这就是我的Node.js应用程序的外观:

#content of index.js    
'use strict';

const http = require('http');
http.createServer((req, res) => {
  res.writeHead(200, {
    'Content-Type': 'text/html; charset=utf-8',
  });
  res.write('<h1>I’m a Node app!</h1>');
  res.end(3000, 'localhost')
}).listen();
console.log('http://example.com:3000/');
#index.js的内容
"严格使用",;
const http=require('http');
http.createServer((req,res)=>{
文书标题(200{
“内容类型”:“text/html;charset=utf-8”,
});
res.write('我是一个节点应用!');
res.end(3000,‘本地主机’)
}).听();
console.log('http://example.com:3000/');

我想知道如何将Node.js应用程序绑定到带有SSL的端口上,并使用现有的Nginx配置。

您必须使用Nginx作为nodejs应用程序的反向代理

例如,使节点在端口3000上运行,并在nginx配置中执行类似操作

如果节点应用程序是服务器中唯一的东西

server {
   listen 443;
   <-- snip -->

   location / {
      proxy_pass http://localhost:3000;
   }

   <-- snip -->
}

这是我对这个问题的回答。 有两个错误:

1) 编辑并添加到
服务器
关闭文件:

location ~ ^/(nodeApp|socket\.io) {
   proxy_pass http://localhost:3000;
}
默认情况下,SocketIO使用/socket.io路径,因此需要配置Nginx,以便它不仅代理/nodeApp请求,还代理/socket.io

2) 在Node.js服务器文件中再进行一次编辑。替换:

http.createServer((req, res) => {` to `app.get('/node', function(req, res) {

您必须将NodeJS应用程序绑定到另一个端口,然后让Nginx代理请求。PS:搜索
nginx反向代理nodejs
,您将找到大量关于如何设置它的示例。@razvanz谢谢!这就是我的问题:)好吧!可以。@razvanz最终它正在侦听
location/app2{}
,但显示错误502网关错误谢谢您的回答:)我需要web服务器的反向代理和反向代理。我已经公布了我的答案。
http.createServer((req, res) => {` to `app.get('/node', function(req, res) {