Created
September 2, 2025 15:58
-
-
Save audetcameron/d0ce164d11bc795fd07435a44b62325a to your computer and use it in GitHub Desktop.
Nginx Config - WIP - multiple Django projects - 80, 443, and 8001
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ``` | |
| #helping in stack overflow : https://stackoverflow.com/questions/79752899/nginx-no-ssl-certificate-is-defined-for-the-listen-ssl-directive-on-custom?noredirect=1#comment140708885_79752899 | |
| #I have not had a chance to test this - | |
| # Server block for redirecting HTTP on port 80 to HTTPS on port 443 and handle www to non www | |
| # Redirect http://myproject.com,http://www.myproject.com => to https://myproject.com | |
| server { | |
| listen 80; | |
| server_name myproject.com; | |
| return 301 https://$host$request_uri; | |
| } | |
| # Redirect www to non www and https (note $host will include www so I hard coded the path here) | |
| server { | |
| listen 80; | |
| server_name www.myproject.com; | |
| return 301 https://myproject.com$request_uri; | |
| } | |
| # Server block for handling HTTPS on port 443 | |
| server { | |
| listen 443 ssl; | |
| server_name myproject.com; | |
| ssl_certificate /etc/letsencrypt/live/myproject.com/fullchain.pem; | |
| ssl_certificate_key /etc/letsencrypt/live/myproject.com/privkey.pem; | |
| location = /favicon.ico { access_log off; log_not_found off; } | |
| location /static/ { | |
| alias /home/myname/myproject/staticfiles/; | |
| } | |
| location /media/ { | |
| alias /home/myname/myproject/media/; | |
| access_log off; | |
| expires 1h; | |
| } | |
| location / { | |
| include proxy_params; | |
| proxy_pass http://127.0.0.1:8000; | |
| } | |
| } | |
| # Server block for redirecting HTTP on port 8001 to HTTPS on port 8001 | |
| server { | |
| listen 8001; | |
| server_name myproject.com; | |
| return 301 https://$host:8001$request_uri; | |
| } | |
| server { | |
| listen 8001; | |
| server_name www.myproject.com; | |
| return 301 https://myproject.com:8001$request_uri; | |
| } | |
| # Server block for handling HTTPS on port 8001 | |
| server { | |
| listen 8001 ssl; | |
| server_name myproject.com; | |
| ssl_certificate /etc/letsencrypt/live/myproject.com/fullchain.pem; | |
| ssl_certificate_key /etc/letsencrypt/live/myproject.com/privkey.pem; | |
| # Optional per-app static/media (comment out if not used) | |
| location /static/ { alias /home/myname/project2/staticfiles/; } | |
| location /media/ { alias /home/myname/project2/media/; access_log off; expires 1h; } | |
| location / { | |
| include proxy_params; | |
| proxy_pass http://127.0.0.1:9001; #is this 9001 or 8001? | |
| # proxy_set_header Host $host:$server_port; | |
| # proxy_set_header X-Real-IP $remote_addr; | |
| # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
| # proxy_set_header X-Forwarded-Proto https; | |
| } | |
| } | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment