Nginx和Express可同时提供静态文件和节点应用

时间:2018-07-16 10:57:06

标签: express ubuntu nginx

我在/var/www/html/admin有我的nodejs应用程序,在/var/www/html/index.html有我的静态网站

在/ admin内部,我有两个文件夹/ p​​ublic-角前端应用程序和/ server Express应用程序,用于我的API。

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上拥有我的静态公共网站,并在www.example.com/admin上运行我的nodejs应用。

1 个答案:

答案 0 :(得分:1)

仅意识到Nginx将提供所有静态内容,包括angularjs one,而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根目录/ var / www / html中定义的根目录下;现在我可以从www.mydomain.com/public访问它,并且API可以正常工作。