如何在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:
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 toa.mydomain.com, and select AppA as the Default Web Module - For AppB: Name it
b-virtual-server, Host Nameb.mydomain.com, Default Web Module AppB - For AppC: Name it
c-virtual-server, Host Namec.mydomain.com, Default Web Module AppC - For WebService: Name it
api-virtual-server, Host Nameapi.mydomain.com, Default Web Module WebService
- For AppA: Name it
- 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.
- 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
Hostheader. 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.
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-servername insun-web.xmlexactly 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
Hostheader is being passed correctly (you can check Payara’s access logs to confirm)
内容的提问来源于stack exchange,提问作者Emmanuel Santana

