You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Payara服务器中配置子域名与虚拟服务器?

Hey there! I’ve tackled this exact setup with GlassFish/Payara before, so let’s get your four web apps mapped to their subdomains smoothly. Here’s a step-by-step breakdown tailored to Payara Server 4:

1. Create Virtual Servers via Payara Admin Console

Payara uses virtual servers to route requests based on the Host header (which your subdomains will send). Here’s how to set them up:

  • Log into your Payara admin console (default URL: http://your-server-ip:4848)
  • Navigate to Configurations > server-config > Virtual Servers (use your custom config if you’re not using the default server-config)
  • Click New to create a virtual server for each app:
    • For AppA: Name it a-virtual-server, set Host Name to a.mydomain.com, and select AppA as the Default Web Module
    • For AppB: Name it b-virtual-server, Host Name b.mydomain.com, Default Web Module AppB
    • For AppC: Name it c-virtual-server, Host Name c.mydomain.com, Default Web Module AppC
    • For WebService: Name it api-virtual-server, Host Name api.mydomain.com, Default Web Module WebService
  • Save each configuration after creating the virtual server

Each of your Java web apps needs to explicitly associate itself with its virtual server. Do this by adding/editing the sun-web.xml (or glassfish-web.xml) file in the app’s WEB-INF directory:

Example for AppA’s sun-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
<sun-web-app>
    <!-- Set context root to / so the subdomain points directly to the app root -->
    <context-root>/</context-root>
    <!-- Match the virtual server name we created in the admin console -->
    <virtual-server>a-virtual-server</virtual-server>
</sun-web-app>

Repeat this for the other three apps, updating the virtual-server value to match their respective virtual server names.

3. Configure DNS and Reverse Proxy (If Applicable)
  • DNS Setup: Make sure your domain registrar has A records pointing each subdomain (a.mydomain.com, b.mydomain.com, etc.) to your Payara server’s public IP address.
  • Reverse Proxy (Optional but Recommended): If your Payara server isn’t directly exposed to the internet (e.g., you’re using Nginx or Apache as a front-end), configure the proxy to forward requests to Payara while preserving the Host header. Here’s a quick Nginx example:
server {
    listen 80;
    server_name a.mydomain.com;

    location / {
        proxy_pass http://localhost:8080; # Point to Payara's HTTP port
        proxy_set_header Host $host; # Critical for virtual server routing
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Duplicate this server block for each subdomain, updating the server_name field accordingly.

4. Restart Payara and Test

Save all your configurations, then restart the Payara domain to apply changes. You can do this via the admin console or with the command line:

asadmin restart-domain domain1

Once the server is back up, test each subdomain in a browser or via curl—they should load the corresponding web app directly.

Quick Troubleshooting Tips

  • Double-check that the virtual-server name in sun-web.xml exactly matches what you created in the admin console
  • Ensure Payara’s HTTP listener is bound to 0.0.0.0 (so it accepts requests from all interfaces)
  • If using a proxy, verify the Host header is being passed correctly (you can check Payara’s access logs to confirm)

内容的提问来源于stack exchange,提问作者Emmanuel Santana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:48:08