Jam*_*ber 10 javascript apache ssl reverse-proxy node.js
我正在尝试让一个全新的基于云的服务器与默认版本的 20.04 服务器 ubuntu 一起使用,并与 apache 和节点一起使用。节点服务器似乎正在运行,没有报告 4006 端口已打开的问题。但我相信我的 apache 配置不是。该请求将挂起很长一段时间。节点终端中不显示任何错误。因此,错误肯定出在我的 apache 配置中,因为我们收到以下 apache 错误并且没有 JS 错误。
502 proxy error
Run Code Online (Sandbox Code Playgroud)
[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: localhost
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
[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
Run Code Online (Sandbox Code Playgroud)
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)
})
Run Code Online (Sandbox Code Playgroud)
/etc/apache2/mods-enabled/proxy.conf /etc/apache2/mods-enabled/proxy.load /etc/apache2/mods-enabled/proxy_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/
Run Code Online (Sandbox Code Playgroud)
我无法准确预测究竟会发生什么,可能是 NodeJS 应用程序崩溃并且不再运行,或者存在配置错误的 Apache 文件。但我坚信,这种情况可以通过从高层开始做事来解决。
此步骤将完成更新 unbuntu 软件包、安装所需的应用程序、配置 Apache 文件以及使用 NodeJS 和 Apache 设置反向代理。
只要不要碰你的 NodeJS 文件和其他与代码相关的应用程序,它们就会是安全的。您也可以进行备份以确保安全。ubuntu 服务器上的其他正在运行的应用程序示例数据库应用程序(例如MySQLas)将很好并且仍在运行。
1.首先我们需要更新ubuntu软件包并安装Apache和NodeJS
$ sudo apt update
$ sudo apt install apache2 npm
Run Code Online (Sandbox Code Playgroud)
2.运行此命令使我们能够使用Apache作为反向代理服务器
sudo a2enmod proxy proxy_http rewrite headers expires
Run Code Online (Sandbox Code Playgroud)
3. 创建Apache虚拟主机文件。
此命令将允许您使用 ubuntu 终端作为文本编辑器,按照终端的指南和提示进行编写。
将“yourSite.com”更改为您网站的域。文件名并不重要。但我认为最好以您的网站域名命名,以便您可以识别它。
$ sudo nano /etc/apache2/sites-available/yourSite.com.conf
Run Code Online (Sandbox Code Playgroud)
4. 使用nano编辑器为您的站点编写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>
Run Code Online (Sandbox Code Playgroud)
5. 禁用默认的 Apache 站点并启用新站点。
$ sudo a2dissite 000-default
$ sudo a2ensite example.com.conf
Run Code Online (Sandbox Code Playgroud)
6. 重新启动 Apache 服务器以应用更改
sudo systemctl restart apache2
Run Code Online (Sandbox Code Playgroud)
我们已经准备好了,因为我们已经将 Apache 设置为反向代理,但我们还需要安装项目的 npm 包,然后运行 NodeJS 应用程序。
7.剩下的步骤都是与NodeJS部署相关的。您可能已经知道这些步骤。
$ sudo apt update
$ sudo apt install apache2 npm
Run Code Online (Sandbox Code Playgroud)
您的 Apache 和 NodeJS 服务器现已启动并运行
尝试通过在浏览器地址栏中输入您的网站域名来访问您的网站
| 归档时间: |
|
| 查看次数: |
20025 次 |
| 最近记录: |