在nginx中强制HTTPS会破坏静态公用文件夹

时间:2019-02-01 14:55:52

标签: node.js nginx

第一个问题是因为我通常可以很容易地找到答案,但这使我感到困惑。我正在使用Nginx在Ubuntu 16.04上运行多个服务器块和Node JS应用程序,并且都运行良好,但是有一个站点我想强制使用运行节点应用程序的HTTPS。当我在nginx中配置服务器块时,可以很好地强制重定向,但是我的静态公共文件夹(用于存储CSS和JS文件夹的地方)不可用,并且我得到了404作为指向我的CSS文件的链接。我对nginx相当陌生,我知道我可能会缺少一些基本知识,但似乎找不到。

这是我的节点js文件:

var express         = require("express"),
    app             = express(),
    bodyParser      = require("body-parser");

    app.use(bodyParser.urlencoded({extended: true}));
    app.set("view engine", "ejs");
    app.use(express.static("public"));

    var indexRoutes     = require("./routes/index")

    app.use("/", indexRoutes);
    app.listen(3600,function(){
       console.log("Blog Site server has started");
    });

此nginx配置正常工作,但不强制使用HTTPS:

server {
    listen 80;
    listen [::]:80;
    index index.html index.htm;

    listen 443 ssl;
    listen [::]:443 ssl;

    ssl on;
    ssl_certificate /etc/ssl/certificate.crt;
    ssl_certificate_key /etc/ssl/private.key;

    root /var/www/book/html;

    index index.html index.htm index.nginx-debian.html;

    server_name www.myblog.com;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;
            proxy_pass http://www.myblog.com:3600;
            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;
    }
}

这会强制使用HTTPS,但不会应用CSS,并且在从公用文件夹加载任何内容时会出现404错误:

server {
    listen 80;
    listen [::]:80;
    server_name myblog.com www.myblog.com;
    return 301 https://www.myblog.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name myblog.com;
    return 301 https://www.myblog.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    ssl on;
    ssl_certificate /etc/ssl/certificate.crt;
    ssl_certificate_key /etc/ssl/private.key;
    root /var/www/book/html;
    index index.html index.htm index.nginx-debian.html;
    server_name www.myblog.com;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            proxy_pass http://www.myblog.com:3600;
            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;
    }
} 

0 个答案:

没有答案