Nginx force http to https On 443 https Port

sample script nginx :

server
{

listen 443 default ssl;
listen [::]:443 ssl;
root /var/www/html/api_mobile/public;

include snippets/ssl-params.conf;

add_header X-Frame-Options “SAMEORIGIN”;
add_head…


This content originally appeared on DEV Community and was authored by Teddy Zugana

sample script nginx :

server 
{

        listen 443 default ssl;
        listen [::]:443 ssl;
        root /var/www/html/api_mobile/public;

        include snippets/ssl-params.conf;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";
        index index.html index.php index.htm index.nginx-debian.html;

        server_name sitename.com;

        charset utf-8;
        location / {
             if ($scheme ="http") {
                # redirect all non api traffic to https block
                return 301 https://$server_name$request_uri;
             }           

            try_files $uri $uri/ /index.php?$query_string;
        }

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }


        error_page 404 /index.php;
        location ~ \.php$ {
            fastcgi_pass unix:/run/php/php8.2-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
        }

    ssl_certificate /etc/letsencrypt/live/sites/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/site/privkey.pem; # managed by Certbot
}

point on scheme condition :

if ($scheme ="http") 
{
                # redirect all non api traffic to https block
                return 301 https://$server_name$request_uri;
}  

it core of redirect on one port 443 https force http to https


This content originally appeared on DEV Community and was authored by Teddy Zugana


Print Share Comment Cite Upload Translate Updates
APA

Teddy Zugana | Sciencx (2024-11-08T04:24:55+00:00) Nginx force http to https On 443 https Port. Retrieved from https://www.scien.cx/2024/11/08/nginx-force-http-to-https-on-443-https-port/

MLA
" » Nginx force http to https On 443 https Port." Teddy Zugana | Sciencx - Friday November 8, 2024, https://www.scien.cx/2024/11/08/nginx-force-http-to-https-on-443-https-port/
HARVARD
Teddy Zugana | Sciencx Friday November 8, 2024 » Nginx force http to https On 443 https Port., viewed ,<https://www.scien.cx/2024/11/08/nginx-force-http-to-https-on-443-https-port/>
VANCOUVER
Teddy Zugana | Sciencx - » Nginx force http to https On 443 https Port. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/08/nginx-force-http-to-https-on-443-https-port/
CHICAGO
" » Nginx force http to https On 443 https Port." Teddy Zugana | Sciencx - Accessed . https://www.scien.cx/2024/11/08/nginx-force-http-to-https-on-443-https-port/
IEEE
" » Nginx force http to https On 443 https Port." Teddy Zugana | Sciencx [Online]. Available: https://www.scien.cx/2024/11/08/nginx-force-http-to-https-on-443-https-port/. [Accessed: ]
rf:citation
» Nginx force http to https On 443 https Port | Teddy Zugana | Sciencx | https://www.scien.cx/2024/11/08/nginx-force-http-to-https-on-443-https-port/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.