Apache Proxy and Daphne : slow connection - django

I am using Django 3.2.3, Apache and Daphne. Daphne is very slow.
I use Apache like a proxy to send the request to Daphne :
<VirtualHost *:443>
AllowEncodedSlashes On
ServerName mysite.com
ServerAdmin admin#gmail.com
ProxyPass "/" "http://127.0.0.1:8001/"
ProxyPassReverse "/" "http://127.0.0.1:8001/"
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET" env=CORS
Header set Access-Control-Allow-Credentials "false" env=CORS
SSLCertificateFile /etc/letsencrypt/live/****/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/***/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
I launch Daphne with the command :
daphne -p 8001 asgi:application
When I access to my website, the log of Daphne are :
2021-06-04 21:17:17,821 INFO Adding job tentatively -- it will be properly scheduled when the scheduler starts
2021-06-04 21:17:17,865 INFO Added job "my_job" to job store "default"
2021-06-04 21:17:17,866 INFO Scheduler started
2021-06-04 21:17:17,909 INFO Starting server at tcp:port=8001:interface=127.0.0.1
2021-06-04 21:17:17,912 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2021-06-04 21:17:17,913 INFO Configuring endpoint tcp:port=8001:interface=127.0.0.1
2021-06-04 21:17:17,919 INFO Listening on TCP address 127.0.0.1:8001
127.0.0.1:34650 - - [04/Jun/2021:21:18:07] "GET /fr/" 200 234920209
127.0.0.1:34698 - - [04/Jun/2021:21:18:56] "GET /fr/jsi18n/" 304 -
It's working, I can display the webpage but it's very very slow, 30sec +
I have no idea of what to search, where to search ...
edit ---
Maybe the problem comes from the Apache proxy but I use 127.0.0.1, I do not see why it could be slow. ping 127.0.0.1 is fast.
edit 2 ---
Using dig and nslookup on 127.0.0.1 / localhost / SERVER ADDRESS is fast.
Can you help me maybe with some ideas ? or the solution :)
Thank you

Finally I have found.
The problem was not from apche but from my django code. I do not know exactly because I have a made a lot of tests but I think this is because the redis server was not launch.
Thank you

Related

How to deploy NuxtJs Application on Apache and access it with IP-URL instead of a domain?

Please note, this is my first nuxt-app trying to deploy on a staging server. We have a staging AWS server where many web applications are deployed as follows.
http://0.0.0.0/app1/public (Laravel)
http://0.0.0.0/app2 (static)
http://0.0.0.0/app3/public/api (Lumen)
http://0.0.0.0/app4/public (VueJs Static)
etc
http://0.0.0.0/app5 (Tried NuxtJs Universal SSR) failed
Note: SERVER: AWS instance has Ubuntu 18.04 with apache2
Now I want to deploy NuxtJs Universal Application (SSR), I have studied many articles, tried but failed.
What I have done so far:
Moved my all code to /var/www/html/app5
followed all the steps https://nuxtjs.org/docs/2.x/deployment/deployment-pm2
stuck, what to do next, and don't know how to access it in a browser with IP server.
pm2 list, command gives me the following result.
Progress Update:
I have finally found a new way to configure it on the main IP with virtual host configuration:
<VirtualHost *:80>
ServerName http://0.0.0.0
DocumentRoot /var/www/html/app5
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
My steps to configure NuxtJs app on IP-URL:
Created a new virtual host as above named nuxtjs.conf
a2dissite 000-default.conf, it was pointing to /var/www/html
a2ensite nuxtjs.conf
a2enmod proxy_http and a2enmod proxy
systemctl restart apache2
Yahoo! my main server IP-URL (http://0.0.0.0) was pointed to NuxtJs-application running perfectly :-)
But the problem was my all other websites stopped because they were running with 000-default.conf virtual host.
Please guide, what should be configuration, I don't want to close my all websites, they should be running as I mentioned at the start as http://0.0.0.0/name-of-web. How can I run the NuxtJs application as ip-url/app-name?
After a long time, my question was alone, so I decided to give it a partner with my answer, part a joke I have found the solution, and the following is my finding. Please update it if you have the more appropriate knowledge.
Enabled 81 port via AWS console.
Added following configuration in the nuxt.config.js to run the application on a specific port locally in the server.
server: {
port: 43411
}
Updated virtual host as follows.
NameVirtualHost *:81
<VirtualHost *:81>
ServerName http://0.0.0.0
DocumentRoot /var/www/html/app5
ProxyPass / http://localhost:43411/
ProxyPassReverse / http://localhost:43411/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now I can access my nuxt-app on an IP URL as follows, along with all other deployed websites on this server.
http://0.0.0.0:81/

Apache throws ERROR 500: Internal Server Error when GET from localhost/internal network

I have a production server with apache and django installed using mod_wsgi.
The django application has a REST API that serves some info when a GET request is sent.
This has always worked fine on the develop server, were we ran django using manage.py in a screen. Now we created a production server with apache running django but this API returns Error 500 when running wget from localhost or other machines in the same network (using 192.168.X.X IP).
Here's the output from wget:
~$ wget localhost:80/someinfo
--2020-04-02 16:26:59-- http://localhost/someinfo
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2020-04-02 16:26:59 ERROR 500: Internal Server Error.
It seems that the connection succeeds, so I guess it's not an apache problem. The error comes from the API response.
The error in apache error.log looks like this:
127.0.0.1 - - [02/Apr/2020:14:24:36 +0000] "GET /someinfo HTTP/1.1" 500 799 "-" "Wget/1.19.4 (linux-gnu)"
question: what is the number after 500? Sometimes is 799 and other times is 803.
But if the request is done using the public IP of the server from outside (i.e. from the browser) the API works fine and I see the correct information.
I already checked django's allowed hosts and it was accepting localhost, and the 192.168.X.X IP of the other machine. In the end I left django's settings.py like this:
#ALLOWED_HOSTS = ['localhost', '127.0.0.1', '192.168.1.101']
ALLOWED_HOSTS = ['*']
Note: 192.168.1.101 is the machine that tries to make the GET request.
The final goal of all this is to be able to make a GET request from a python script running in that machine (which already works if django runs via manage.py).
My apache.conf:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
#DocumentRoot /var/www/html
Alias /static /home/myuser/myproject/django/static_root
<Directory /home/myuser/myproject/django/static_root>
Require all granted
</Directory>
<Directory /home/myuser/myproject/django/myproject_django>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-home=/home/myuser/env python-path=/home/myuser/myproject/django
WSGIProcessGroup myproject
WSGIScriptAlias / /home/myuser/myproject/django/myproject_django/wsgi.py
</VirtualHost>
I tried running django via manage.py and the wget from localhost works just fine. The problem only appears when django is ran by apache.
I also tried the solution given in this post, but changing the line does not fix the error.
I have some doubts concerning this error:
how does apache run django?
does restarting apache2 service also restart django? (thus, reading again the settings.py)
Is there any other django settings file rather than the one I'm editing?
how can I see django logs? I don't have the console now so I can't see real time prints.
I appreciate a lot any help.
I finally managed to solve it myself.
It turns out wsgi handles requests from localhost or external IPs as different instance groups. So all I had to do is put
WSGIApplicationGroup %{GLOBAL}
in /etc/apache2/sites-available/000-default.conf

How to run daphne in localhost with https and mkcert

I am trying to run a django-channels project locally using https (the app has a facebook login that requires https).
I have followed the instructions for generating a key and certificate using mkcert ( https://github.com/FiloSottile/mkcert ) and have attempted to use the key and certificate by running daphne -e ssl:443:privateKey=localhost+1-key.pem:certKey=localhost+1.pem django_project.asgi:application -p 8000 -b 0.0.0.0
The server seems to be starting OK however when I try to visit https://0.0.0.0:8000 nothing happens and eventually I get a 'took too long to respond' message.
No new output is added to the standard daphne output that appears when I start up the server:
2019-07-16 19:23:27,818 INFO HTTP/2 support enabled
2019-07-16 19:23:27,818 INFO Configuring endpoint ssl:8443:privateKey=../sec/localhost+1-key.pem:certKey=../sec/localhost+1.pem
2019-07-16 19:23:27,823 INFO Listening on TCP address 0.0.0.0:8443
2019-07-16 19:23:27,823 INFO Configuring endpoint tcp:port=8000:interface=0.0.0.0
2019-07-16 19:23:27,824 INFO Listening on TCP address 0.0.0.0:8000
Can anyone help with this?
You should map the 8000 host port to port 443 of the container while runnig the server.
docker run ... -p 8000:443 ...
Turns out that setting up the Twisted ssl stuff overrides the port that you're setting up in daphne, so in the example above, the site would be shown on port 443

OSRM: Server not responding

I am unable to get a valid response from my server (Ubuntu 16.04) at DigitalOcean after installing OSRM succefully. I have followed the instructions here:
https://github.com/Project-OSRM/osrm-backend/wiki/Running-OSRM
When running the following:
osrm-routed netherlands-latest.osrm
I am getting the following output in my console:
[info] starting up engines, v5.18.0
[info] Threads: 2
[info] IP address: 0.0.0.0
[info] IP port: 5000
[info] http 1.1 compression handled by zlib version 1.2.8
[info] Listening on: 0.0.0.0:5000
[info] running and waiting for requests
Now when I try to access my server at port 5000:
http://123.456.789.000:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true
Is not giving me any response (timeout). I tried the actual domain name as well.
I have also tried to setup a reverse proxy. But apache is also not giving me any logs. Note that this is in my config (/etc/apache2/sites-available/000-default.conf):
<VirtualHost *:8080>
ServerName my.domain.nl
ProxyPass / http://localhost:5000
ProxyPassReverse / http://localhost:5000
ProxyRequests Off
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I have also verified that no firewall is running. Any idea what I could be missing here?
Go to your apache2.conf file and then paste the following line:
ProxyPass /osrm http://localhost:5000
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Then type this command to stop and start your apache2:
sudo service apache2 stop
sudo service apache2 start
After that go to your browser and check the link below:
https://your_server_ip_or_domain_name/osrm/table/v1/driving/13.388860,52.517037;13.385983,52.496891?annotations=distance
This works for me.

Invalid address when startup gunicorn

I encounterd an error when bound gunicorn listen address to my nginx server,here is the thing:
1.gunicorn_wsgi.py
import multiprocessing
bind="192.168.239.145:8080"(my nginx server ip address)
workers = multiprocessing.cpu_count() 2 + 1
2.nginx.conf
http{
upstream realservers{
server 192.168.239.146:8080;(my django and gunicorn address)
}
servr{
listen 80;
server_name example.com
location / {
proxy_pass http://realservers
}
}
}
when i ran gunicorn -c gunicorn-wsgi.py myproject.wsgi,error occur:
[2015-03-30 04:56:05 -0700] [38656] [INFO] Starting gunicorn 19.3.0
[2015-03-30 04:56:05 -0700] [38656] [ERROR] Invalid address: ('192.168.239.145', 8080)
I noticed that gunicorn has mentioned that if you are running nginx on a different host than gunicorn you need to tell gunicorn to trust the the x-forward-* headers sent by nginx.
If you are running Nginx on a different host than Gunicorn you need to
tell Gunicorn to trust the X-Forwarded-* headers sent by Nginx. By
default, Gunicorn will only trust these headers if the connection
comes from localhost. This is to prevent a malicious client from
forging these headers:
gunicorn -w 3 --forwarded-allow-ips="10.170.3.217,10.170.3.220" test:app
I followed what it said ,but still same error. And I change the address to 127.0.0.1 and 0.0.0.0 ,they work fine,but is not secure,How to configure it ,please help me !
It's obviously not your problem, but it is confusing that you're calling your configuration file "gunicon_wsgi.py". It's not your WSGI file. Call it "gunicorn_conf.py" or something similar.
Your problem is however that you have misunderstood what it means to bind to an IP address. You can't bind your gunicorn server to an IP address on a different machine; that makes no sense at all. And it's not "insecure" to bind to 0.0.0.0.