gpt4 book ai didi

node.js - Nginx 反向代理与主机名不匹配

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:50 28 4
gpt4 key购买 nike

我目前在 Apache 中定义了一个简单的 API:

<VirtualHost *:80>
ServerName http://exampleapi.org
ServerAlias http://exampleapi.org
ProxyPreserveHost On
ProxyPass /api http://localhost:3000
</VirtualHost>

由于各种原因我需要将其迁移到 Nginx,所以在 /etc/nginx/conf.d/<domain>.conf我选择了:

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

server_name http://exampleapi.org;
# API endpoint
location = / {
proxy_pass http://127.0.0.1:4000;
}
}

问题似乎是我在 /etc/nginx/conf.d/<domain2>.conf 中有另一个文件/站点正如我从访问日志中可以清楚地看到的那样,它总是匹配的。那么其他配置中的哪里与所有内容相匹配呢? (请注意,像 example.com 这样的站点名称已被 <domain> 混淆)。

server {
server_name SITE_URL <domain>;
server_tokens off;

access_log /var/log/nginx/access.log;

# Max request size
client_max_body_size 20M;
large_client_header_buffers 4 256k;

root /usr/local/learninglocker/current/webapp/ui/dist/public;

# xAPI endpoints
location ~* ^/data/xAPI(.*)$ {
proxy_pass http://127.0.0.1:8081/data/xAPI$1$is_args$args;
}

# API endpoints
location = /api {
rewrite /api / break;
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
}

location ~* ^/api(.*)$ {
proxy_pass http://127.0.0.1:8080$1$is_args$args;
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;
}

# All other traffic directed to statics or Node server
location / {
try_files $uri @node_server;
}

# Node UI server
location @node_server {
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;
}

# Load configuration files for the default server block.
error_page 404 /404.html;
location = /40x.html {
root /usr/share/nginx/html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# We don't need .ht files with nginx.
location ~ /\.ht {
deny all;
}

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/<domain>/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/<domain>/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 = <domain>) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80;
listen [::]:80;
server_name SITE_URL <domain>;
return 404; # managed by Certbot
}

最佳答案

server_name 指令是要查找的主机 header 值。它不包括协议(protocol)。

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

server_name exampleapi.org;
# API endpoint
location / {
proxy_pass http://127.0.0.1:4000;
}
}

另外,请注意,我将 location =/ 更改为 location/,因为使用 = 它只会匹配该确切路径,而不会匹配我认为不是计划的其他路径。

关于node.js - Nginx 反向代理与主机名不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58118415/

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