我在http:/ myapp:8008 / service上有一个域名(仅举例)。我想使用Nginx和此服务器的SSL功能设置反向代理,其请求由位于/opt/somefolder/myapp.
的NodeJs应用程序(侦听端口5505)处理
我安装了Nginx并且我的NodeJs应用程序已在运行但不确定如何为此服务器设置服务器块(应用程序不在/usr/local/var/www
)并编辑文件/etc/nginx/nginx.conf
文件以满足我的需求。我尝试使用从此网站找到的配置
https://coderwall.com/p/hwkjba/install-configure-node-js-nginx-on-ubuntu
与此站点一起设置SSL:
这对我没有用。
答案 0 :(得分:1)
1)使用以下内容创建文件myapp.conf
:
server {
listen 443;
client_max_body_size 2048M;
client_body_buffer_size 2048M;
disable_symlinks off;
server_name yourdomain.com www.yourdomain.com;
root /opt/somefolder/myapp/public; # or remove it
index index.html index.htm;
ssl on;
ssl_certificate /opt/somefolder/myapp/certs/server.crt;
ssl_certificate_key /opt/somefolder/myapp/certs/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
ssl_session_cache shared:SSL:1m;
location @app {
log_not_found off;
access_log off;
proxy_pass https://127.0.0.1:8443;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}
location / {
try_files $uri $uri/ @app;
}
}
2)复制或符号链接myapp.conf
文件到/etc/nginx/sites-enabled/myapp.conf
3)在nodejs app中听https:
var
express = require('express'),
https = require('https'),
fs = require('fs'),
path = require('path'),
app = express();
var httpsServer =
https.createServer({
key: fs.readFileSync(path.join(__dirname, 'certs', 'server.key')),
cert: fs.readFileSync(path.join(__dirname, 'certs', 'server.crt'))
}, app);
httpsServer.listen(8443, '127.0.0.1', function() {
console.log('App listening');
});
4)重启nginx
5)使用forever
或pm2