How to remove port number from Django application - python

I have deployed a Django application to virtual server using nginx as proxy server and Gunicorn. The application is binded like
gunicorn --bind example.com:8000 MyApp.wsgi:application
My nginx is configured like
server {
server_name <my_ip_address>;
access_log off;
location /static/ {
alias /opt/examApp/static/admin/;
}
location / {
proxy_pass http://example.com:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
Here the problem is, the application is working fine on www.example.com. But when we use Django password reset, the reset email contains link like this http://example.com:8000/reset/MjA/466-434d4ewe54546878b4/
How to remove that port number 8000 from the link.

Try configuring the sites framework.
PasswordResetForm uses domain by default which is stored in database and can be changed to whatever you need.

Related

Is NGINX blocking flask backend code executing?

My flask web app is based on socket.io, flask, nginx and gunicorn. One of frontend features sends 1.3MB message to the backend using socket.io. I know that from 3.x socket.io maxHttpBufferSize was decreased from 100MB to 1MB so I have max_http_buffer_size limit set to 2MB on the server side. Everything works good, backend receiving message but after few seconds of executing code on backend side it stops without any information in nginx error.log and gunicorn.log. Web browser(Opera, Chrome) console does not return any error too. I think that the problem is on the nginx side because when I execute my app using only gunicorn it works in the right way.
Have you any idea what should I change in my nginx config? Or the problem is elsewhere?
I'm using
Python 3.7.9,
python-engineio 4.0.0,
python-socketio 5.0.2,
Flask-SocketIO 5.0.0,
Javascript SocketIO 3.0.0
gunicorn 20.0.4,
nginx 1.14.0
My .service config file
[Unit]
Description=Gunicorn instance to serve <my_app>
After=network.target
[Service]
User=<my_user>
Group=www-data
WorkingDirectory=/path/to/my/app
Environment="PATH=/path/to/my/env/bin"
ExecStart=/path/to/my/env/bin/gunicorn --certfile=/path/to/my/app/cert.crt --keyfile=/path/to/my/app/key.key --access-logfile /path/to/my/app/gunicorn_logs.log --worker-class eventlet -w 1 --timeout 300 -b 0.0.0.0:5000 <my_app>:app
[Install]
WantedBy=multi-user.target
My nginx proxy requests config
server{
listen 80;
listen [::]:80;
server_name <my_domain> <my_server_ip>;
return 301 https://$host$request_uri;
}
server{
listen 443 default_server ssl;
listen [::]:443 default_server ssl;
access_log /path/to/my/app/access.log;
error_log /path/to/my/app/error.log;
ssl_certificate /etc/letsencrypt/live/<my_domain>/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/<my_domain>/privkey.pem;
server_name <my_domain> <my_server_ip>;
proxy_read_timeout 300;
# reverse proxy for HTTP connection
location / {
include proxy_params;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://0.0.0.0:5000/;
proxy_redirect off;
}
# reverse proxy for Socket.IO connections
location /socket.io {
proxy_pass https://0.0.0.0:5000/socket.io;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
# let's encrypt host validation
location ~ /.well-known{
root /path/to/my/app/static/;
}
}

Python3 + Nginx: Redirecting HTTP traffic to HTTPS on AWS Elastic Beanstalk

I deployed a Python application on Elastic Beanstalk using Nginx as reverse proxy server and uWSGI as interface between Python and Nginx.
The environment has been configured as single instance, so no I'm not using a load balancer.
I already have the SSL certificate, but I don't know how to configure it on my environment.
I need to install the certificate and redirect all the HTTP traffic to HTTPS.
I searched on the Web and found a few links on the AWS official documentation, but all docs are related to Apache web server.
Can you give me an help or a good reference to follow?
Here are the steps which I followed to redirect all HTTP traffic to HTTPS in my AWS Elastic Beanstalk Go environment using Nginx. I believe it should be the same for Python+Nginx.
Created a file in the below directory structure in the root of the application code.
.ebextensions/nginx/conf.d/elasticbeanstalk/00_application.conf
The content of the file was
location / {
set $redirect 0;
if ($http_x_forwarded_proto != "https") {
set $redirect 1;
}
if ($http_user_agent ~* "ELB-HealthChecker") {
set $redirect 0;
}
if ($redirect = 1) {
return 301 https://$host$request_uri;
}
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
For Apache, you should check out this link.

'Welcome' message from Nginx shown when I run my wsgi Django project + Nginx + Gunicorn

I'm beginner in Django and I've finished my first project.
I have an Ubuntu server in Digital Ocean and this is what I've done:
My project nginx configuration file:
server {
server_name domain.com;
access_log off;
location /static/ {
alias /opt/myenv/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
My project is located in /opt/myenv/myenv/
When I execute gunicorn myproject.wsgi it looks like it's running
Listening at: http://127.0.0.1:8000 (1481)
But when I access into my IP, I just see an Welcome message from Nginx.
What is happening?
(Sorry my bad english)
I can't comment (rep points) so will post here.
You have Gunicorn bound to :8000 but have Nginx looking at :8001
Did you put this config in sites-available and symlink to sites-enabled, then remove "default" from sites-enabled? (If it's showing welcome I assume not)
I don't see where you are directing Nginx to listen:
server {
listen 80 default_server;
listen [::]:80;
...
Any reason you are not setting up Gunicorn using a unix sock instead and configuring Nginx to that? Here's a typical config for this:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}

Serving multiple sites with gunicorn and nginx?

I'm trying to serve both a Django and a Flask site using gunicorn and nginx. The Django site is currently available at example.com, and I'd like for the Flask site to be availabe at myapp.example.com .
My nginx configuration file is as follows for the Django site:
cat /etc/nginx/sites-available/example.com
server {
listen 80;
server_name example.com;
location /static {
alias /home/me/sites/example.com/static;
}
location / {
proxy_set_header Host $host;
proxy_pass http://unix:/tmp/example.com.socket;
}
}
I also have a second nginx config file for the Flask site:
cat /etc/nginx/sites-available/myapp.example.com
server {
listen 80;
server_name myapp.example.com;
access_log /var/log/nginx/access.log;
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I'm pretty sure that I want to change my proxy_pass variable, but am not sure what I should change it to.
My gunicorn file currently is as follows:
description "Gunicorn server for example.com"
start on net-device-up
stop on shutdown
respawn
setuid me
chdir /home/rowan/sites/example.com/source
exec ../virtualenv/bin/gunicorn \
--bind unix:/tmp/example.com.socket \
example.com.wsgi:application
Do I need a second gunicorn config file for the Flask site? Which I would then put in /etc/init/ and start through the sudo start gunicorn-myapp.example.com command?

Django using Nginx to serve static content

I am trying to configure nginx to serve the static content for my django project on a remote VPS. I'm using the following configuration for my nginx instance:
server {
server_name myVPSip;
access_log off;
location /static/ {
alias /usr/local/pcat/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
I created the config file in ../nginx/sites-available/, linked it to /sites-enabled/, and restarted nginx however when I hit myip:8001/static I get a django 404.
You shouldn't access your website by 123.123.123.123:8001 because that's the port django is using. You are supposed to access 123.123.123.123 (that is port 80) where nginx is running.

Categories

Resources