Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

HTTP to HTTPS Nginx/Forge

Using Laravel Forge (nginx) I have this in my nginx config:

server {
    listen 80;
    listen 443 ssl;

    server_name crm.domain.com;
    root /home/forge/crm.domain.com/public;
    
    

    # FORGE SSL (DO NOT REMOVE!)
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate /etc/nginx/ssl/crm.domain.com/7947/server.crt;
    ssl_certificate_key /etc/nginx/ssl/crm.domain.com/7947/server.key;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        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; }

    access_log off;
    error_log  /var/log/nginx/default-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

How can I set up a permenant redirect so if a user types,

www.crm.domain.com or crm.domain.com it would just redirect them to https://crm.domain.com ?

I have tried the following but doesn't redirect:

server {
    server_name crm.domain.com;
    rewrite ^/(.*) https://crm.domain.com/$1 permanent;
}
0 likes
8 replies
theUnforgiven's avatar

So could I get away with putting this

listen 80;
        listen [::]:80;

        server_name www.example.com example.com;

        return 301 https://example.com$request_uri;

at the top on my block?

bashy's avatar

You need different server blocks to listen on different server_names without using the evil "if".

Put each of them above each other as it states in the blog post. I just have mine after each other in the same file.

theUnforgiven's avatar

So just adding this to the top won't work then?

listen 80;
    listen [::]:80;
    listen 443 ssl;

    server_name www.crm.domain.com crm.domain.com;
    root /home/forge/crm.domain.com/public;

    return 301 https://crm.domain.com$request_uri;
bashy's avatar
bashy
Best Answer
Level 65

It will if you do this in the same file

server {
    listen 80;
    listen [::]:80; // probably don't need this if you don't want to listen on IPv6

    server_name www.crm.domain.com crm.domain.com;

    return 301 https://crm.domain.com$request_uri;
}

server {
    listen 443 ssl;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate /etc/nginx/ssl/crm.domain.com/7947/server.crt;
    ssl_certificate_key /etc/nginx/ssl/crm.domain.com/7947/server.key;

    server_name www.crm.domain.com;

    return 301 https://crm.domain.com$request_uri;
}

server {
    // original server block
}

Like it is in my post but I've just done it for you... :) https://bashy.im/blog/nginx-redirect-to-https-with-without-www-subdomain

7 likes
theUnforgiven's avatar

Great stuff now I get it, cheers Ben ( @bashy ) needed to see it for my example to understand it, don't ask me why but that's how I tend to learn, kinda like the JS/Ajax stuff you helped with the other day, got that now actually wrote my own today, so was quite impressed with myself.

mehany's avatar

@bashy so for each application on my server I can specify the above configurations and i should not get any issues such as any SSL conflict issues ( not sure even if I am saying this right! ), right? [Edited] While I see this approach works great, and maintainable, when I manage my own servers, sometimes I am obligated to use third party shared hosting servers :( but this raise a question, is there any benefit from handling HTTP traffic from nginx config files VS .htaccess?

bashy's avatar

@lstables That's fine, glad you're learning from all this!

@em57 Well this is a very good way of doing the redirects, it requires you to have access to the virtualhost file for the Nginx site though. Since Nginx doesn't read any sort of file like Apache (.htaccess) before it loads the page, you have to do it in the server block(s). Unless someone else knows of a way.

Nginx is really for the advanced users, if you don't require the features of it just use Apache, it's just as good at processing requests and passing it onto PHP.

Please or to participate in this conversation.