Skip to content

Instantly share code, notes, and snippets.

@meduzen
Last active August 31, 2020 11:17
Show Gist options
  • Save meduzen/339bde026497ebe62bf5932b1d28781a to your computer and use it in GitHub Desktop.
Save meduzen/339bde026497ebe62bf5932b1d28781a to your computer and use it in GitHub Desktop.
Replace Nginx server name on Forge + Digital Ocean

Replace Nginx server name

This works on a Digital Ocean’s server managed by Laravel Forge. What this does:

  1. Power off the server by connecting through ssh and running sudo poweroff.
  2. Take a snapshot of the server in the Digital Ocean panel. If things goes wrong, it’ll help server restauration.
  3. Warning step:
  • Starting step 4, instructions are largely inspired from a Stack Overflow / Server Fault answer.
  • When prompt for choices like “replacing this config file” or “use this grub file”, don’t: always keep what’s currently working. You can review differences (the prompt tell you how), but the chance to not screw something are greater by keeping current things as is.
  1. Turn the server on again from the Digital Ocean panel.
  2. sudo apt remove nginx
  3. sudo apt autoremove
  4. sudo apt purge nginx
  5. sudo apt update
  6. sudo apt upgrade
  7. sudo apt install nginx
  8. Check Nginx is installed: nginx -v
  9. sudo apt install nginx-extras
  10. The module is now automatically loaded because the server is setup (by Forge or by default) that way: /etc/nginx/nginx.conf loads the whole /etc/nginx/modules-enabled directory, where the nginx-extras module has been installed.
  11. Now we are going to edit the Nginx configuration of our website, from Forge (your site / bottom of the page / Files / Edit Nginx Configuration).
  12. In server { …HERE… }, add:
server_tokens off; # remove Nginx version
more_set_headers "Server: MyServerName"; # requires nginx-extras, see https://serverfault.com/questions/954708/install-more-set-headers-in-nginx-1-15-8/954724

Example:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;

    # various other stuff here…

    # your custom Server name here.
    server_tokens off; # remove Nginx version, probably useless along with next line.
    more_set_headers "Server: Thanks meduzen for the tip!";

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

    # still other stuff here…
}
  1. Save the configuration. Forge automatically reboots Nginx.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment