Ask Your Question
2

What is the procedure for redirecting HTTPS requests from port 443 to a different port using Nginx?

asked 2023-06-25 12:29:18 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-25 12:46:02 +0000

djk gravatar image

To redirect HTTPS requests from port 443 to a different port using Nginx, you can follow these steps:

  1. 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.

  2. 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;
    ...
}
  1. Inside the 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.

  1. Save the configuration file and reload Nginx for the changes to take effect:
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).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-25 12:29:18 +0000

Seen: 12 times

Last updated: Jun 25 '23