我正在使用作为服务器。我已经在NGINX PM2服务器上配置了我的SSL证书。当我撞到https://api.test.com时。我可以得到响应,但是当我做http://api.test.com时,我无法得到任何东西(即它识别它为一个未知的URL )。
我该怎么处理?我应该在代码中或者在NGINX PM2中做些什么吗?
码
var express = require('express');
import application from './route';
const port = normalizePort(3000);
const server = http.createServer(application);
server.listen(port);发布于 2020-02-13 09:47:17
最好在nginx配置中处理这个问题。下面是一个很小的例子:
server {
listen xxx.xxx.xxx.xxx:80;
server_name www.example.net;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen xxx.xxx.xxx.xxx:443;
server_name www.example.net;
root /vhosts/www.example.net;
ssl on;
ssl_certificate /etc/pki/nginx/www.example.net.crt;
ssl_certificate_key /etc/pki/nginx/www.example.net.key;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
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 https;
}
}发布于 2020-02-13 09:09:32
您不需要为您的API URL进行任何配置,因此请尝试如下:
const application=require ('./route');
const express = require('express');
const app = express ();
app.use(application);
app.listen(4000,()=>console.log('connected'));发布于 2020-02-13 10:26:45
将以下配置添加到NGINX中:
server {
listen 80;
server_name example.com;
location / {
return 301 https://$host$request_uri;
}
}这将将来自http://{your_domain}的任何请求(假设您没有使用80以外的端口)重定向到https://{your_domain}。
https://stackoverflow.com/questions/60203931
复制相似问题