Skip to content

Instantly share code, notes, and snippets.

@ttodua
Last active October 27, 2024 18:31
Show Gist options
  • Save ttodua/b5f54429c00dad6e052b6ccbda08dcb0 to your computer and use it in GitHub Desktop.
Save ttodua/b5f54429c00dad6e052b6ccbda08dcb0 to your computer and use it in GitHub Desktop.
nginx-wp-ssl

(note, we everywhere use example.com in these samples, but you should replace it with something other your preferred name)

  1. Set virtual host entry in your OS hosts file, like 127.0.0.1 example.com (in Windows, run also ipconfig /flushdns in powershell)

  2. Generate local certificate files by running mkcert: mkcert "example.com" or with wildcard & subdomain support: mkcert "*.example.com" (You can get mkcert very easily: https://github.com/FiloSottile/mkcert#installation )

  3. If you use docker then jump to 4th line, otherwise if you don't use Docker, then you will need to manually setup things in your local LAMP server make an HTTPS entry (443 port) for that example.com domain, with including those ssl files.

  4. If you use docker, then you can use the above docker-compose.yaml, relative to that file location create ./nginx/conf folder and place sample.conf, and create ./nginx/certs folder and place the generated cert files.

  5. Use docker compose up & open https://example.com (you can "bypass" warning to open site).

name: my_services_1
services:
my_wp:
networks:
- my_wp_net
...
...
...
my_nginx:
image: nginx:1.27
ports:
- 80:80
- 443:443
restart: always
volumes:
- ./nginx/conf/:/etc/nginx/conf.d/:ro
- ./nginx/certs:/etc/nginx/ssl
networks:
- my_wp_net
networks:
my_wp_net:
driver: bridge
server {
listen 80;
listen [::]:80;
server_name example.com;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://my_wp:80;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/_wildcard.example.com.pem;
ssl_certificate_key /etc/nginx/ssl/_wildcard.example.com-key.pem;
location / {
proxy_set_header Host $host; # $http_host
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $realip_remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_pass http://my_wp:80; # no need to use like 'http://host.docker.internal:80'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment