Nginx上游到express服务器-502错误
嘿,我在数字海洋上有一台nginx服务器。我曾经用PM2在AWS上做主机,但调整后用nginx运行 问题是我好像得了502分。有些东西配置不正确。最初,我只是让客户机接受服务,这很有效,但当我切换到服务器时,它也执行502 客户端和服务器文件夹位于同一父目录中 这是我当前的var/nginx/sites available/defaultNginx上游到express服务器-502错误,express,nginx,proxy,Express,Nginx,Proxy,嘿,我在数字海洋上有一台nginx服务器。我曾经用PM2在AWS上做主机,但调整后用nginx运行 问题是我好像得了502分。有些东西配置不正确。最初,我只是让客户机接受服务,这很有效,但当我切换到服务器时,它也执行502 客户端和服务器文件夹位于同一父目录中 这是我当前的var/nginx/sites available/default # Main Content Delivery Block (SSL) server { listen 443 ssl;
# Main Content Delivery Block (SSL)
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name quakeviz.app;
ssl on;
ssl_certificate /etc/ssl/certs/mpaccione_ssl.crt;
ssl_certificate_key /etc/ssl/private/mpaccione_ssl.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
add_header Content-Security-Policy upgrade-insecure-requests;
location / {
root /var/www/html/usgs_viz/server;
proxy_pass https://quakeviz.app:8080/;
proxy_ssl_session_reuse on;
#try_files $uri $uri/ /;
}
#location /bufferLength {
# root /var/www/html/usgs_viz/server;
# proxy_pass https://quakeviz.app:8080/;
# proxy_ssl_session_reuse on;
#}
#location /quakeData {
# root /var/www/html/usgs_viz/server;
# proxy_pass https://quakeviz.app:8080/;
# proxy_ssl_session_reuse on;
#}
}
# Redirect
#server {
# listen 80 default_server;
# listen [::]:80 default_server;
# listen 443 ssl;
# listen [::]:443 ssl;
#
# return 301 https://quakeviz.app$request_uri;
#}
这是服务器文件夹中的index.js。我现在得到一个关于客户端和api的502(更新问题)
// Modules
const cors = require('cors'),
express = require('express'),
expressStaticGzip = require('express-static-gzip'),
fs = require('fs'),
path = require('path'),
app = express(),
// Globals
getDirectories = (source) => {
return fs
.readdirSync(source, { withFileTypes: true })
.filter((dir) => dir.isDirectory())
.map((dir) => dir.name)
}
// CORS for Local Testing
app.use(cors())
// Compression
app.use(
'/',
expressStaticGzip(path.join(__dirname, '../client/build'), {
enableBrotli: true,
orderPreference: ['br', 'gz'],
})
)
// Routes
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '../client/build', 'index.html'))
})
app.get('/.well-known(/*)?', function (req, res) {
res.sendFile(path.join(__dirname, '../.well-known', 'assetlinks.json'))
})
app.get('/privacy-policy', function (req, res) {
res.sendFile(path.join(__dirname, '../privacy_policy.html'))
})
// API
app.get('/bufferLength', function (req, res) {
const encoding = req.headers['accept-encoding'],
compArr = getDirectories(
path.join(__dirname, '/api-data/compressed/')
).sort(function sortNum(a, b) {
return b - a
})
if (compArr.length < 2) {
console.warn('ByteLength Not Available')
res.status(500).send(new Error('ByteLength Not Available'))
} else {
console.log('BUFFER LENGTH RES')
fs.readFile(
path.join(
__dirname,
`/api-data/compressed/${compArr[1]}/byteLength.json`
),
(err, data) => {
if (err) {
console.warn(err)
res.status(500).send(new Error(err))
} else {
console.log(data)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(data)
}
}
)
}
})
app.get('/quakeData/:index', function (req, res) {
const encoding = req.headers['accept-encoding'],
index = req.params.index,
compArr = getDirectories(
path.join(__dirname, '/api-data/compressed/')
).sort(function sortNum(a, b) {
return a - b
})
// Send Second Newest Dataset as Latest May hvae Read/Writes
if (compArr.length <= 1) {
console.warn('Unsupported Content Encoding Headers')
res.status(500).send(new Error('Dataset Not Currently Available'))
} else {
if (encoding.includes('br')) {
console.log('BROTLI RES')
fs.readFile(
path.join(
__dirname,
`/api-data/compressed/${compArr[1]}/brotliData${index}.txt.br`
),
(err, data) => {
if (err) {
console.warn(err)
res
.status(500)
.send(new Error('Brotli Compression Data Read Error'))
} else {
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Encoding': 'br',
})
res.end(data)
}
}
)
} else if (encoding.includes('gzip')) {
console.log('GZIP RES')
fs.readFile(
path.join(
__dirname,
`/api-data/compressed/${compArr[1]}/gzipData${index}.txt.gz`
),
(err, data) => {
if (err) {
console.warn(err)
res.status(500).send(new Error('Gzip Compression Data Read Error'))
} else {
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Encoding': 'gzip',
})
res.end(data)
}
}
)
} else {
console.warn('Unsupported Content Encoding Headers')
res.status(415).send(new Error('Unsupported Requested Encoding Type'))
}
}
})
// Listen
app.listen(8080, () => console.log('API listening on 8080'))
//模块
const cors=require('cors'),
express=require('express'),
expressStaticGzip=require('express-static-gzip'),
fs=需要('fs'),
path=require('path'),
app=express(),
//全球的
GetDirectory=(源)=>{
返回fs
.readdirSync(源,{withFileTypes:true})
.filter((dir)=>dir.isDirectory())
.map((dir)=>dir.name)
}
//用于本地测试的CORS
app.use(cors())
//压缩
应用程序使用(
'/',
expressStaticGzip(path.join(_dirname,'../client/build'){
enableBrotli:是的,
orderPreference:['br','gz'],
})
)
//路线
app.get('/',函数(req,res){
res.sendFile(path.join(_dirname,“../client/build”,“index.html”))
})
app.get('/.well-known(/*)?',函数(req,res){
res.sendFile(path.join(_dirname,“...well-known”,“assetlinks.json”))
})
app.get('/privacy policy',函数(req,res){
res.sendFile(path.join(_dirname,'../privacy_policy.html'))
})
//原料药
app.get('/bufferLength',函数(req,res){
const encoding=req.headers['accept-encoding'],
compArr=getDirectories(
join(uu dirname,“/api data/compressed/”)
).sort(函数sortNum(a,b){
返回b-a
})
如果(比较器长度<2){
console.warn('bytellength不可用')
res.status(500).send(新错误('ByTeleLength不可用'))
}否则{
log('BUFFER LENGTH RES')
fs.readFile(
path.join(
__dirname,
`/api数据/compressed/${compArr[1]}/byteLength.json`
),
(错误,数据)=>{
如果(错误){
控制台。警告(错误)
资源状态(500).发送(新错误(err))
}否则{
console.log(数据)
res.writeHead(200,{'Content-Type':'application/json'})
res.end(数据)
}
}
)
}
})
app.get('/quakeData/:index',函数(req,res){
const encoding=req.headers['accept-encoding'],
索引=请求参数索引,
compArr=getDirectories(
join(uu dirname,“/api data/compressed/”)
).sort(函数sortNum(a,b){
返回a-b
})
//发送第二个最新数据集,作为最新的hvae读/写数据集
如果(比较器长度{
如果(错误){
控制台。警告(错误)
物件
.地位(500)
.send(新错误('Brotli压缩数据读取错误'))
}否则{
文书标题(200{
“内容类型”:“应用程序/json”,
“内容编码”:“br”,
})
res.end(数据)
}
}
)
}else if(encoding.includes('gzip')){
console.log('GZIP RES')
fs.readFile(
path.join(
__dirname,
`/api数据/compressed/${compArr[1]}/gzipData${index}.txt.gz`
),
(错误,数据)=>{
如果(错误){
控制台。警告(错误)
res.status(500).send(新错误('Gzip压缩数据读取错误'))
}否则{
文书标题(200{
“内容类型”:“应用程序/json”,
“内容编码”:“gzip”,
})
res.end(数据)
}
}
)
}否则{
console.warn('不支持的内容编码头')
res.status(415).send(新错误('Unsupported Requested Encoding Type'))
}
}
})
//听
app.listen(8080,()=>console.log('API监听8080'))
不明白您为什么要尝试代理服务的每个路由,为什么不让应用程序为您路由请求
例如:
location / {
root /var/www/html/usgs_viz/server;
proxy_pass https://quakeviz.app:8080/;
proxy_ssl_session_reuse on;
}
我注意到的另一件事是代理通行证上的https,我认为这行不通,试着用http替换它。我改成了这个。我还使用PM2运行了服务器 我开始更多地了解fullstack系统管理部分,所以我并不知道我需要在PM2上运行它,以及使用Nginx路由它。我有一个想法,如果我指向它,Nginx将运行它。有点傻,但我确实认为这里的nginx配置更好。见下文
# Main Content Delivery Block (SSL)
server {
listen 443 ssl;
server_name quakeviz.app;
ssl on;
ssl_certificate /etc/ssl/certs/mpaccione_ssl.crt;
ssl_certificate_key /etc/ssl/private/mpaccione_ssl.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
add_header Content-Security-Policy upgrade-insecure-requests;
location / {
root /var/www/html/usgs_viz/server;
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
删除那些
listen443ssl代码>和侦听[:]:443 ssl代码>第二个服务器块中的行。nginx-t
命令说明了什么?与502状态代码对应的访问日志和错误日志条目是什么?@IvanShatsky nginx-t语法很好。。。我试图强制从www路由到非www,因为ssl不覆盖www。我是否仍应将其从第二个服务器块中删除?@RichardSmith错误日志说。。。[错误]31844#31844:*1219 connect()在连接到上游时失败(111:连接被拒绝)。。。请求:“GET/bufferLength HTTP/1.1”上游:“https://quakeviz.app”更新了问题。是的,我在PM2上进行了此设置,看起来我没有完成nginx的配置。我已经更新了问题,并将整个服务切换到express服务器。现在客户端构建也不起作用。这是一个502的一切。。。