我正在尝试让一台全新的基于云的服务器使用默认版本的20.04服务器ubuntu来处理apache和节点。节点服务器似乎在运行,没有报告4006端口打开的问题。然而,我相信我的apache配置不是。这项要求将被搁置很长时间。节点终端中没有显示错误。因此,错误肯定在我的apache配置中,因为我们得到了下面的apache错误,而没有JS错误。
一段时间后请求错误
502 proxy errorApache错误日志
[Sun Oct 17 20:58:56.608793 2021] [proxy:error] [pid 1596878] (111)Connection refused: AH00957: HTTP: attempt to connect to [::1]:4006 (localhost) failed
[Sun Oct 17 20:58:56.608909 2021] [proxy_http:error] [pid 1596878] [client 207.46.13.93:27392] AH01114: HTTP: failed to make connection to backend: localhostvhost
<VirtualHost IP_ADDRESS:80>
ServerName api.aDomain.com
Redirect permanent / https://api.aDomain.com/
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost IP_ADDRESS:443>
ServerName api.aDomain.com
ProxyRequests on
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
ProxyPass / http://localhost:4006/
ProxyPassReverse / http://localhost:4006/
#certificates SSL
SSLEngine on
SSLCACertificateFile /etc/ssl/api.aDomain.com/apimini.ca
SSLCertificateFile /etc/ssl/api.aDomain.com/apimini.crt
SSLCertificateKeyFile /etc/ssl/api.aDomain.com/apimini.key
ErrorLog ${APACHE_LOG_DIR}/error_api.aDomain.com.log
CustomLog ${APACHE_LOG_DIR}/access_api.aDomain.com.log combined
</VirtualHost>
</IfModule>终端输出
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `babel-node -r dotenv/config --inspect=9229 index.js`
Debugger listening on ws://127.0.0.1:9229/c1fcf271-aea8-47ff-910e-fe5a91fce6d2
For help, see: https://nodejs.org/en/docs/inspector
Browserslist: caniuse-lite is outdated. Please run next command `npm update`
Server ready at http://localhost:4006节点服务器
import cors from 'cors'
import scrape from './src/api/routes/scrape'
const express = require('express')
const { ApolloServer, gql } = require('apollo-server-express')
const { postgraphile } = require('postgraphile')
const ConnectionFilterPlugin = require('postgraphile-plugin-connection-filter')
const dbHost = process.env.DB_HOST
const dbPort = process.env.DB_PORT
const dbName = process.env.DB_NAME
const dbUser = process.env.DB_USER
const dbPwd = process.env.DB_PWD
const dbUrl = dbPwd
? `postgres://${dbUser}:${dbPwd}@${dbHost}:${dbPort}/${dbName}`
: `postgres://${dbHost}:${dbPort}/${dbName}`
var corsOptions = {
origin: '*',
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
}
async function main() {
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
}
const server = new ApolloServer({ typeDefs, resolvers })
const app = express()
app.use(cors(corsOptions))
app.use(
postgraphile(process.env.DATABASE_URL || dbUrl, 'public', {
appendPlugins: [ConnectionFilterPlugin],
watchPg: true,
graphiql: true,
enhanceGraphiql: true,
})
)
server.applyMiddleware({ app })
//Scraping Tools
scrape(app)
const port = 4006
await app.listen({ port })
console.log(` Server ready at http://localhost:${port}`)
}
main().catch(e => {
console.error(e)
process.exit(1)
})Apache Mods启用
/etc/apache2/mods-启用/proxy.conf/etc/apache2/mods-启用/proxy.load/etc/apache2/mods-启用/代理_http.load
更新错误日志
[Thu Oct 21 10:59:22.560608 2021] [proxy_http:error] [pid 10273] (70007)The timeout specified has expired: [client 93.115.195.232:8963] AH01102: error reading status line from remote server 127.0.0.1:4006, referer: https://miniatureawards.com/
[Thu Oct 21 10:59:22.560691 2021] [proxy:error] [pid 10273] [client 93.115.195.232:8963] AH00898: Error reading from remote server returned by /graphql, referer: https://miniatureawards.com/发布于 2021-10-26 23:58:17
我无法准确预测到底会发生什么,这可能是NodeJS应用程序崩溃,不再运行,或者存在配置错误的Apache文件。但我坚信,这种情况将通过从头做起来解决。
这个步骤将通过更新unbuntu包、安装所需的应用程序、配置Apache文件以及使用NodeJS和Apache设置反向代理。
只是不要接触您的NodeJS文件和其他与代码相关的应用程序,它们将是安全的。您也可以备份,以确保。在该ubuntu服务器示例数据库应用程序上运行的其他应用程序(如MySQL )将很好,而且仍在运行。
1.首先我们需要更新ubuntu软件包并安装Apache和NodeJS。
$ sudo apt update
$ sudo apt install apache2 npm2.运行此命令,使我们能够使用Apache作为反向代理服务器。
sudo a2enmod proxy proxy_http rewrite headers expires3.创建一个Apache虚拟主机文件.
此命令将允许您使用ubuntu终端作为文本编辑器,遵循该终端的指南和提示进行编写。
注意:
用站点的域更改"yourSite.com“。不重要的是文件的名称。但我认为最好用你的网站域名来命名它,这样你才能识别它。
$ sudo nano /etc/apache2/sites-available/yourSite.com.conf使用编辑器为您的站点编写Apache文件。
注意:这部分很关键,所以请注意。
用站点域名更改ServerName和ServerAlias。
ProxyPass和ProxyPassReverse有两个参数。
第一个是反斜杠"/" -这是您的NodeJS应该位于的绝对路径,并且由于它的单个反斜杠,这意味着它的主目录。
第二个是NodeJS应用程序的url "http://127.0.0.1:3000/"。注意它的端口"3000“,您可能需要用您在NodeJS应用程序中使用的端口替换它。
<VirtualHost *:80>
ServerName example.com // replace this with site domain name without www at the beginning
ServerAlias www.example.com // replace this with site domain name beginning with www. + yourdomainname + .com
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:30000/
</VirtualHost>5.禁用默认的Apache站点并启用新的.
$ sudo a2dissite 000-default
$ sudo a2ensite example.com.conf6.重新启动Apache以应用更改
sudo systemctl restart apache2现在我们可以准备好将Apache设置为反向代理,但我们还需要安装项目的npm包,然后运行NodeJS应用程序。
7.步骤的其余部分都与NodeJS部署相关。您可能已经知道这一步骤。。
// install npm packages
npm install
// for a better experience using NodeJS in production install pm2 globally
npm install -g pm2
// Then run your NodeJS application using pm2 command
pm2 start // you should be at root of your NodeJS project folder when running this command
// run this another pm2 command to make sure your NodeJS app will re-run when it encounter downtime.
$ pm2 save
$ pm2 startup您的Apache和NodeJS服务器已经启动并正在运行。
尝试通过在浏览器地址栏中输入站点域名来访问站点。
发布于 2022-06-30 21:35:34
在主要情况下,这是由selinux引起的(当您有RHEL或CentOS时):
# setsebool -P httpd_can_network_connect 1也请检查:
machines
/etc/hosts file中的主机名。
发布于 2021-10-20 23:51:34
如果您在节点服务器上使用了一个停靠程序,那么它可能会被错误地设置。
https://stackoverflow.com/questions/69609078
复制相似问题