-2

What's the best way to set up nginx conf to force https on the index.php page only and every other page be forced to http.

thanks

3
  • 2
    What options have you tried? How well did each work? Plus I'm curious why you'd want to do this.
    – Tim
    Dec 22, 2016 at 22:09
  • Why don't you want to secure all your pages? I hope you don't handle any personnal data, because it's required by law to protect them.
    – Tom
    Dec 23, 2016 at 10:32
  • A bit of an overreaction on your part, all data is handled securely, virtually the whole site is ssl, login, myaccount etc... I am setting it up this way as it's a url shortener and there no reason why the shortened url needs to be ssl as well as it's just a gateway to the users intended url anyway, also if i go ssl with that as well it would limit my advertisement options. this is no different to how adfly does things.
    – Sherif
    Dec 23, 2016 at 22:45

1 Answer 1

1

Try something like this should come close to working. I haven't tested it so there could be typos, but it should at least give you some guidance.

# You might address PHP some other way
upstream php {
  server 127.0.0.1:9001;
}

server {
  server_name www.example.com example.com;
  listen 443 ssl http2;
  ssl_certificate /path/fullchain;
  ssl_certificate_key /path/privkey;

  # the index.php?$args is for wordpress, you can remove it
  try_files $uri $uri/ /index.php?$args; 

  # Single URL
  location = /index.php {
    # plus other PHP settings and parameters
    fastcgi_pass php; 
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  # Every other URL
  location / {
    return 301 http://www.example.com$request_uri;
  }

  # Run PHP scripts
  location ~ \.php$ {
    # plus other PHP settings and parameters
    fastcgi_pass php; 
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

# Insecure server
server {
  server_name www.example.com example.com;
  listen 80 default_server;

  # the index.php?$args is for wordpress, you can remove it
  try_files $uri $uri/ /index.php?$args; 

  location = /index.php {
    return 301 https://www.example.com/index.php;
  }

  location / {
    root /var/www/site;
  }

  location ~ \.php$ {
    # plus other PHP settings and parameters
    fastcgi_pass php; 
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .