gpt4 book ai didi

express - Nginx 和 Express 为静态文件和节点应用程序提供服务

转载 作者:行者123 更新时间:2023-12-04 18:44:56 25 4
gpt4 key购买 nike

我的 nodejs 应用程序位于 /var/www/html/admin和我的静态网站 /var/www/html/index.html
在/admin 中,我有两个文件夹/public - Angular 前端应用程序和/server 用于我的 API 的 express 应用程序。

server.js 文件如下所示:

const express = require("express");
const MongoClient = require("mongodb").MongoClient;
var morgan = require("morgan"); // used to see requests
const bodyParser = require("body-parser");
var mongoose = require("mongoose");
var config = require("./config");
var path = require("path");
const app = express();

const port = 3000;

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST");
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,content-type, Authorization"
);
next();
});

// log all requests to the console
app.use(morgan("dev"));

// connect to our database (hosted on modulus.io)
mongoose.connect(config.database);

var apiRoutes = require("./routes/routes")(app, express);
app.use("/api", apiRoutes);

// set static files location
// used for requests that our frontend will make
app.use(express.static('public'));

// MAIN CATCHALL ROUTE ---------------
// SEND USERS TO FRONTEND ------------
// has to be registered after API ROUTES
/*app.get("*", function(req, res) {
res.sendFile(path.join(__dirname + "./../public/index.html"));
});*/

app.listen(port, () => {
console.log("We are live on " + port);
});

不,我有如下所示的 nginx conf 文件:
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
listen 443 ssl default_server;
#ssl on;
ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_prefer_server_ciphers on;
# listen [::]:443 ssl default_server
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name example.com;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
root /var/www/html;
try_files $uri $uri.html $uri/index.html;



}

location /admin/ {
proxy_pass http://localhost:3000/;
rewrite ^/admin /$1 break;
try_files $uri $uri/ =404;
}
}

我不确定如何设置这两个,以便在 www.example.com 上拥有我的静态公共(public)网站并让我的 nodejs 应用程序在 www.example.com/admin 上运行。

最佳答案

刚刚意识到 Nginx 将提供包括 angularjs 在内的所有静态内容,而 express 将负责 API。

所以我删除了 server.js 中的行来提供静态文件,我像这样编辑了 nginx conf

server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
listen 443 ssl default_server;
#ssl on;
ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_prefer_server_ciphers on;
# listen [::]:443 ssl default_server
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name example.com;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
root /var/www/html;
try_files $uri $uri.html $uri/index.html @express;



}

location @express {
proxy_pass http://localhost:3000/;

}
}

然后我将我的/public 文件夹移动到 nginx root/var/www/html 中定义的根目录中;现在我可以从 www.mydomain.com/public 访问它,并且 API 可以正常工作。

关于express - Nginx 和 Express 为静态文件和节点应用程序提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51360199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com