API基础地址为IP时,Nginx的server_name应如何配置?
Absolutely! Nginx fully supports using an IP address as the server_name value. This is a common setup when accessing services directly via IP (instead of a domain name), which aligns perfectly with your use case here.
For your specific API endpoint http://13.20.32.76:8000/api/users/list/, you should set server_name to 13.20.32.76. When a request hits that IP on port 8000, Nginx will check the Host header (which will be 13.20.32.76 in this scenario) and match this server block correctly.
Here’s your updated Nginx config snippet with the correct server_name:
server { listen 8000; server_name 13.20.32.76; access_log /data/ldl/logs/13.20.32.76.access.log main; location /index01.html { alias /data/repo/Qi02/index01.html; } # Don’t forget to add a location block for your API endpoint if needed! # Example for proxying API requests: # location /api/ { # proxy_pass http://your_api_backend; # } }
A quick side note: If you want this server block to handle all incoming requests on port 8000 (regardless of the Host header), you can use server_name _; as a catch-all. But using the specific IP is better for precision, especially if you have multiple server blocks running on the same port.
内容的提问来源于stack exchange,提问作者qg_java_17137

