使用NGINX和HTTPS进行快速路由设置?

时间:2018-12-06 14:32:38

标签: node.js express nginx

简而言之:我遇到的问题与之前在此问题(How to setup routes with Express and NGINX?)中遇到的问题相同,尽管由于使用HTTPS而我的文件看起来略有不同,但我从未使用过try_files

正常的根链接有效(my.website.org),但是当我导航至(my.website.com/login)时,我从NGINX获得了404

所有站点本身都可以正常工作(当我关闭登录索引时也是如此),但是一旦我不访问根链接,我就会收到404 :(。我已经尝试了几种建议的解决方案但是无法为我做任何工作。也许有人在这里对我的设置提出建议。


我的项目的文件结构:

my
| - public
| - views
|     | - index.pug
|     L - login.pug
| - app.js
L - package.json

首先打开我的 app.js 文件:

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();

// Load View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

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

// Home Route
app.get('/', function(req, res){res.render('index');});
// Login Route
app.get('/login', function(req, res){res.render('login');});

// Start Server
app.listen(3000, function(){console.log('Server started on port 3000...');});

下一步我的NGINX配置:

server {
    listen 80;
    server_name my.website.org;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    server_name my.website.org;

    access_log  /var/log/nginx/my.access.log;
    error_log   /var/log/nginx/my.error.log;

    location = / {
        proxy_redirect off;
        proxy_pass http://localhost:3000;
        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;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
        root /var/www/my/public;
        access_log off;
        expires max;
    }

    ssl_certificate /myfullchain.pem;
    ssl_certificate_key /mykey.pem;
}

0 个答案:

没有答案