我试图在Nginx Ubuntu 17.04上使用SSL在端口上设置 Node.js应用程序生产。到目前为止,我已经启动并运行SSL Nginx
服务器。
这就是我的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/');
我想知道如何在具有SSL的端口上使用现有的Nginx配置绑定此Node.js应用程序。
答案 0 :(得分:1)
You have to use nginx as a reverse proxy to your nodejs application.
For example, make node run on port 3000, and do something like this in your nginx conf.
If the node app is the only thing in the server,
server {
listen 443;
<-- snip -->
location / {
proxy_pass http://localhost:3000;
}
<-- snip -->
}
If you have something else like a php application running on the server, create another server block and give a separate server_name
for your app.
server {
listen 443;
server_name nodeapp.mysite.com;
<-- snip -->
location / {
proxy_pass http://localhost:3000;
}
<-- snip -->
}
答案 1 :(得分:0)
这是我对问题的回答。 有两个错误:
1)编辑并添加server
关闭文件:
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) {