使用Nginx将所有目录传递给Express

yftpprvb  于 2023-01-08  发布在  Nginx
关注(0)|答案(1)|浏览(188)

我尝试通过我的应用程序中的所有目录来表达,在某些情况下,目录包含一个动态的'键',我会:捕获。
我甚至无法获得一个已定义的子目录/pretest来传递express。尽管为Location /复制了相同的选项,但它为我提供了Ngxin 404
理想情况下,我希望将domain.com/prepop/{lookup_key}传递给本地主机:8080/prepop/{lookup_key}
其中,{lookup_key}每次都是不同的值
你知道我做错了什么吗?任何/所有的帮助都非常感谢。
密码-
server.js

const path = require('path');
const express = require("express");
const mysql = require('mysql2');
const cors = require("cors")

const port = process.env.PORT || 8080;

const app = express();

app.use(cors())

app.get('/', (req, res) => {

    res.send('hello');
    
});

app.get('/pretest', (req, res) => {

    res.send('test');
    
});

app.get('/prepop/:lookup_key', (req, res) => {

    let connection = mysql.createConnection({
 //commented out for stackoverflow
    });

    connection.connect();

    connection.query('SELECT * FROM table_incoming_leads WHERE leadid = '+req.params.send_key, (err, response, fields) => {
        if (err) console.log(err);

        res.json({
            email: response[0].email,
            first_name: response[0].first_name,
            last_name: response[0].last_name
        })
    });

    connection.end();

        

    

})


app.listen(port, () => console.log(`Server listening on port ${port}`));

/etc/nginx/可用站点/默认

server {

    root /var/www/html;


    location / {
        proxy_pass http://localhost:8080/;
        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;       
try_files $uri $uri/ =404;
    }

    location /pretest {

        proxy_pass http://localhost:8080;
        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;               
try_files $uri $uri/ =404;
        }

    location /prepop {

        proxy_pass http://localhost:8080/;
        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;               
try_files $uri $uri/ =404;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/subdomain.mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/subdomain.mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = subdomain.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80 ;
    listen [::]:80 ;
    server_name subdomain.mydomain.com;
    return 404; # managed by Certbot

}
gj3fmq9x

gj3fmq9x1#

你需要从/prepop位置删除try_files指令,因为当nginx收到一个请求,比如/prepop/somekey,它会在你的根目录中寻找一个同名的文件(somekey),因为没有这样的文件,它会返回404。

相关问题