gpt4 book ai didi

nginx - Nginx多个服务器 block 监听相同的端口

转载 作者:行者123 更新时间:2023-12-03 11:01:33 25 4
gpt4 key购买 nike

我想在同一端口www.example.com上运行api.example.com80

这就是我所拥有的。我所有的Google Ping都导致以下代码。但是,这不起作用。

server {
listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;

root /var/www/example.com/html/example/app;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name www.example.com www.example.org;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}

location /bower_components {
alias /var/www/example.com/html/example/bower_components;
}

location /scripts {
alias /var/www/example.com/html/example/scripts;
}

location /content {
alias /var/www/example.com/html/example/content;
}

location /api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:3836;
}
}

server {
listen 80
server_name api.example.com

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:3836;
}
}

我不知道原因。有什么建议吗?

谢谢!

最佳答案

/etc/nginx/sites-available/www.example.com/etc/nginx/sites-available/api.example.com中分别创建两个文件(您不必这样做,但是会更清楚)

api.example.com文件的内容

server {
listen 80
server_name api.example.com
root /var/www/api.example.com/html/example/app; #also add a root dir here
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:3836;
}
}

www.example.com的内容:
server {
listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;

root /var/www/example.com/html/example/app;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name www.example.com www.example.org;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}

location /bower_components {
alias /var/www/example.com/html/example/bower_components;
}

location /scripts {
alias /var/www/example.com/html/example/scripts;
}

location /content {
alias /var/www/example.com/html/example/content;
}

location /api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:3836;
}
}

最后启用域: sudo ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.comsudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/api.example.com

关于nginx - Nginx多个服务器 block 监听相同的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33055212/

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