1 | initial version |
To redirect HTTPS requests from port 443 to a different port using Nginx, you can follow these steps:
Open the Nginx configuration file in a text editor. The location of the configuration file may vary depending on the operating system and distribution you are using. The most common locations are /etc/nginx/nginx.conf
or /usr/local/nginx/conf/nginx.conf
.
Locate the server
block that handles HTTPS requests on port 443. It should look something like this:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
...
}
server
block, add a location
block that matches the root URL /
and sends a 301 redirect to the new port:location / {
return 301 https://example.com:8443$request_uri;
}
In this example, we are redirecting to port 8443, but you should replace it with the actual port number you want to use.
sudo nginx -t # Test the configuration
sudo systemctl reload nginx # Reload Nginx
Now, any HTTPS requests to the root URL (e.g., https://example.com/
) will be redirected to the new port. Note that this configuration only affects HTTPS requests on port 443, not HTTP requests on port 80 or other ports. If you want to redirect HTTP requests as well, you will need to add a server
block that listens on port 80 and redirects to HTTPS on port 8443 (or whichever port you are using).