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

fsluijs's avatar

Nginx / Forge: should not redirect to https from http

Hi guys,

A little unusual situation here, I guess. Which is why I was not able to find the answer online (yet).

I am working on a redirect application. I have added an SSL certificate with Forge and that all works fine. I just found that there is a step in the middle that I want to remove.

Right now what happens is: http://old.com >301> https://old.com >301> https://new.com What I would like is http://old.com >301> https://new.com

I tried creating two server blocks, one for port 80 and one for 443, where the port 80 one did not have the SSL settings. But that did not make a difference.

Any help or advise is very much appreciated!

  • Frank

My nginx config is:

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mydomain.com/before/*;

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    
    server_name _;
    root /home/forge/mydomain.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/mydomain.com/466676/server.crt;
    ssl_certificate_key /etc/nginx/ssl/mydomain.com/466676/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/mydomain.com/server/*;

    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/mydomain.com-error.log error;

    error_page 404 /index.php;

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

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mydomain.com/after/*;
0 likes
7 replies
aurawindsurfing's avatar

Hi,

I like to make my life simple so have a look; https://twitter.com/laravelphp/status/967060155564199937?lang=en

You can set it up directly withing Forge without touching nginx config.

I would however go a step further and do yourself a favour and enable free cloudflare for your domain, you can get ssl certs, page rules and lots of other goodies in there and set up what you are looking for in no time with Page Rules.

Hope it helps!

fsluijs's avatar

Hi @aurawindsurfing and thanks for the reply.

I am not looking to do just a couple of redirects myself, but am building an application where my clients can manage their own redirect rules. That is why I am building this application and why I want to resolve this extra http to https step (SEO team thinks that removing this step in between is better for SEO).

aurawindsurfing's avatar

Hi,

Yes I was told it was removed from Forge some time ago and it does impact SEO greatly as without it google will see www and non www versions of your app's pages.

Why not try to utilise the Forge API and set up rules that way?

fsluijs's avatar

I didn't consider the Forge API to be honest, but thinking of it, two main reasons not to:

  1. I want to build the service independant of Forge or another service, so if it seems best to later switch to another platform or management console, I can.
  2. I want to offer the platform as pay-per-use, so I will need to keep logs of usage per domain.
aurawindsurfing's avatar

In theory, you could modify your nginx configs from your app code using shell_exec

like so

 shell_exec('bash -c "echo 'text to append' >>ngnix.site.config"');

But that feels just wrong to me.

fsluijs's avatar
fsluijs
OP
Best Answer
Level 2

I found the redirect that I didn't want to occur.

The config has a before folder that gets included, which had the http to https redirect.

include forge-conf/mydomain.com/before/*;

Problem solved. Thanks for your help and the conversation :)

Please or to participate in this conversation.