将域名映射到NGINX子目录且保留浏览器域名的配置求助
解决方案:NGINX多域名访问配置调整
1. 修改默认Server配置
编辑/etc/nginx/sites-available/default,将泛域名匹配改为明确指向mydomain,避免干扰其他域名的请求处理:
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # 仅匹配mydomain的请求 server_name mydomain; include /etc/nginx/sites-available/subdirs/*; }
2. 调整WebBoss子目录配置
编辑/etc/nginx/sites-available/subdirs/webboss,修改根路径跳转规则为仅针对mydomain生效,同时为boss-test.com.nl添加根路径代理逻辑:
# 保留原有/webboss路径配置,确保mydomain/webboss正常访问 location /webboss { include proxy_params; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header SCRIPT_NAME /webboss; proxy_redirect off; proxy_connect_timeout 1200s; proxy_send_timeout 1200s; proxy_read_timeout 1200s; fastcgi_send_timeout 1200s; fastcgi_read_timeout 1200s; if (!-f $request_filename) { proxy_pass http://unix:/srv/webboss/run/webboss.sock; break; } } # 修改根路径处理逻辑:分域名处理 location / { # 仅当访问mydomain根目录时,跳转至mydomain/webboss if ($host = mydomain) { return 301 http://$host/webboss; } # 处理boss-test.com.nl等其他域名的根路径请求,直接代理WebBoss服务 include proxy_params; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 根路径访问无需设置SCRIPT_NAME前缀 proxy_set_header SCRIPT_NAME ""; proxy_redirect off; proxy_connect_timeout 1200s; proxy_send_timeout 1200s; proxy_read_timeout 1200s; fastcgi_send_timeout 1200s; fastcgi_read_timeout 1200s; if (!-f $request_filename) { proxy_pass http://unix:/srv/webboss/run/webboss.sock; break; } }
3. 验证配置并重启NGINX
执行命令验证配置语法正确性:
sudo nginx -t
若提示test is successful,重启NGINX使配置生效:
sudo systemctl restart nginx
效果说明
- 访问
http://mydomain会自动跳转至http://mydomain/webboss,地址栏保留mydomain域名 - 访问
http://boss-test.com.nl或http://boss-test.com.nl/webboss均直接加载WebBoss内容,地址栏保持boss-test.com.nl域名不变 - 原有的
http://mydomain/webboss访问路径正常工作
内容的提问来源于stack exchange,提问作者Ruben
相关产品推荐
相关产品推荐

