To properly redirect subdomains in Nginx, you can use the "server" configuration block in your Nginx configuration file (usually located at /etc/nginx/nginx.conf
Here's an example:
Open your Nginx configuration file in a text editor:
sudo nano /etc/nginx/nginx.conf
Inside the http block, find or create a new server block for the main domain. It should look something like this:
server_name example.com www.example.com;
Add a new server block below the main domain configuration for the subdomain you want to redirect. Use the server_name directive to specify the subdomain:
return 301 http://example.com$request_uri;
In this example, we're redirecting the subdomain subdomain.example.com to http://example.com using a 301 permanent redirect. You can modify the return directive to redirect to a different URL or use different redirect codes as per your requirements.
Restart Nginx for the changes to take effect:
sudo systemctl restart nginx
After making these changes, any requests to the specified subdomain will be redirected according to the configuration you've set up.