Redirect HTTP to HTTPs in Nginx


DreamFactory can run atop any modern web server, however Nginx is the most popular of the bunch. Naturally you'll want to keep communications between the client and server secure, and the easiest way to do so is by ensuring all traffic is transmitted over HTTPS. With just a few lines of code added to the Nginx configuration file you can redirect all HTTP traffic to the correct HTTPS URL.

Your Nginx web server will refer to a configuration file to determine where log files are stored, what port will be used for traffic (typically 80 and 443), and what to do should non-2XX status codes occur. This file is typically named default.conf however when the server is hosting multiple sites it might be named something like Generally (but not always), these configuration files are found in the /etc/nginx/conf.d directory.

To redirect HTTP traffic to HTTPS, you'll add a separate server block to the file which listens on port 80, and then returns a 301 redirect status code along with the requested host and request URI, but using the HTTPS protocol.

server {
    listen 80;
    return 301 https://$host$request_uri;

server {
    listen 443 ssl

Need API Advice?

Our team has advised thousands of companies around the world on API projects. Go to market faster by talking to the API experts.


Ready to get started?