I just cant wrap my head around how to properly write a nginx.conf file. My nginx container in docker keeps giving me error
2022/10/03 12:33:28 [error] 31#31: *1 open() "/usr/src/app/staticblog/style.css" failed
(2: No such file or directory), client: 172.18.0.1, server: ,
request: "GET /static/blog/style.css HTTP/1.1",
host: "localhost:1337", referrer: "http://localhost:1337/profile/login"
nginx.conf file
upstream blog {
server back:8000;
}
server {
listen 80;
location /{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://blog;
}
location /static/ {
alias /usr/src/app/static;
}
location /media/ {
alias /usr/src/app/media;
}
}
I feel like im just writing conf file wrong, but I cant wrap my head around directories in docker and how to properly manage them
UPDATE: as #HemalPatel mentioned, I messed some slashes. Now I think I might as well got confused with setting.py file xD
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "blog/static/",
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
Im so sorry its such a dumb question
UPDATE 2:
nginx container error log:
72.21.0.1 - - [03/Oct/2022:13:52:32 +0000] "GET /profile/login HTTP/1.1" 200 8285 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0" "-"
172.21.0.1 - - [03/Oct/2022:13:52:32 +0000] "GET /static/blog/style.css HTTP/1.1" 404 153 "http://localhost:1337/profile/login" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0" "-"
172.21.0.1 - - [03/Oct/2022:13:52:32 +0000] "GET /docs/5.2/assets/brand/bootstrap-logo.svg HTTP/1.1" 404 2542 "http://localhost:1337/profile/login" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0" "-"
Django container log
Not Found: /docs/5.2/assets/brand/bootstrap-logo.svg
Related
I can't work this out, nginx insists on looking in a "static" folder even though I think I have given "staticfiles" as the location. Yet the logs are constantly showing the work "static" in the file path. Here is the nginx.conf:
server {
listen 80;
server_name 10.88.58.95;
location = /favicon.ico { access_log off; log_not_found off; }
root /srv/pcc_django/;
location /staticfiles/ {
}
location / {
proxy_set_header Host $http_host;
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 $scheme;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, "static")
...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
...
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
This is what the access logs are showing:
10.184.52.12 - - [18/Oct/2019:12:58:58 +0000] "GET /static/css/microblog/style.css HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:12:58:58 +0000] "GET /static/images/microblog/platform_control.png HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET / HTTP/1.1" 200 5916 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET /static/css/microblog/style.css HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET /static/images/microblog/platform_control.png HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET /static/css/microblog/style.css HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:03:58 +0000] "GET /static/images/microblog/platform_control.png HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:05:58 +0000] "GET / HTTP/1.1" 200 5916 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:05:58 +0000] "GET /static/css/microblog/style.css HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
10.184.52.12 - - [18/Oct/2019:13:05:58 +0000] "GET /static/images/microblog/platform_control.png HTTP/1.1" 404 77 "http://10.88.58.95/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0" "-"
How do I get nginx to look at the correct folder please, it should be looking at:
/srv/pcc_django/staticfiles/
it's currently looking at:
/srv/pcc_django/static/
which is in the same folder. If I swap the names over it works, so I'm certain that my settings.ph file is correctly configured regarding DEBUG = False which is the current setting on the server.
UPDATE: I have just edited the nginx.conf file to the following:
location = /favicon.ico { access_log off; log_not_found off; }
location /staticfiles {
root /srv/pcc_django;
}
and got this from the error log:
2019/10/21 13:22:00 [error] 5888#0: *1 open() "/srv/pcc_django/staticfiles/static/admin/css/responsive.css" failed (2: No such file or directory), client: 10.184.53.51, server: 10.88.58.95, request: "GET /static/admin/css/responsive.css HTTP/1.1", host: "10.88.58.95", referrer: "http://10.88.58.95/admin/"
I don't understand where the /static/ is coming from, nginx, or django, what can I do to verify where the problem actually is?
Django looks in the static folder provided in setting STATIC_ROOT and STATIC_URL. The first should be "/staticfiles/". The second should match a location in your nginx to map to a server directory.
location /static {
expires 1y;
add_header Cache-Control "public";
alias /some/path/in/your/server/;
}
Django settings was set up correctly, the issue is with nginx.conf adding an alias of the absolute path to my css fixed my issue.
server {
listen 80;
server_name 10.88.58.95;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
alias /srv/pcc_django/staticfiles/;
}
location / {
proxy_set_header Host $http_host;
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 $scheme;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
I have installed nginx successfully and uwsgi work well but when I want to link nginx with uwsgi.it's wrong
This is my nginx.conf and uwsgi.ini
server {
listen 80;
server_name localhost;
#charset koi8-r;
charset utf-8;
#access_log logs/host.access.log main;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
root html;
index index.html index.htm;
}
and
http=127.0.0.1:8000
#socket=/root/project/learning_log/nginx_uwsgi.socket
chdir=/root/project/learning_log/
#chmod-socket=664
master=true
processes=4
threads=2
module=learning_log.wsgi
#wsgi-file=uwsgi_test.py
#stats=127.0.0.1:8000
./nginx & uwsgi3 --ini /root/project/learning_log/uwsgi.ini
when I access 127.0.0.1,it's wrong
and the access.log:
127.0.0.1 - - [15/Jan/2018:18:06:13 +0800] "GET / HTTP/1.1" 504 537 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
the error.log:
2018/01/15 18:06:13 [error] 12986#0: *13 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8000", host: "127.0.0.1"
I am trying to send e-mail, using simple contact form.
On local development it works, but on production I get 404 error and no e-mails are sent.
What I have:
Nginx, Gunicorn, Django
settings.py
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['example.com', 'www.example.com']
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'googleapppassword'
EMAIL_USE_TLS = True
access_log
"POST /contact/ HTTP/1.1" 404 198 "http://example.com/contact/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"
UPDATE:
nginx config
upstream example.com {
server localhost:12345 fail_timeout=0;
}
server {
listen 80;
server_name www.example.com;
rewrite ^/(.*) http://example.com/$1 permanent;
}
server {
listen 80;
client_max_body_size 4G;
server_name example.com;
access_log /path/to/access.log;
keepalive_timeout 5;
root /path/to/static_content;
location ~ ^/(static|media)/ {
access_log off;
expires 60d;
add_header Cache-Control public;
}
location / {
proxy_pass http://example.com;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /path/to/static_content/static/html;
}
location ~ ^/(static|media)/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://example.com;
break;
}
}
}
error.log
2015/03/21 15:36:04 [error] 28486#0: *272 upstream prematurely closed connection while reading response header from upstream, client: 95.65.125.145, server: example.com, request: "POST /contact/ HTTP/1.1", upstream: "http://127.0.0.1:12345/contact/", host: "example.com", referrer: "http://example.com/contact/"
OK, this was hard to solve.
I had to edit my /etc/hosts file and restart services
127.0.0.1 localhost.localdomain localhost example.com
127.0.0.1 localhost
127.0.1.1 example.com
Afer this I've restarted services
$ sudo /etc/init.d/networking restart
$ sudo service nginx restart
$ sudo service postfix restart
Now it's working superfast without any errors.
P.S. postfix is in the list, because I was trying to solve the issue by using postfix and not smtp.gmail.com, but postfix is not required.
Also, take a look at /etc/hostname and /etc/mailname
I have followed this tutorial: http://blog.wercker.com/2013/11/25/django-16-part3.html and I am just trying to make it work locally with Vagrant for now. I am not trying to use Wercker.
After everything is installed, I try to access the website but I get a Bad Request (400) error every time. I do not know if that is due to a problem in nginx or in gunicorn.
They both have a log entry so at least I know that the request goes all the way through gunicorn and is not stopped at the nginx level.
Where is the problem located? Gunicorn? nginx?
Here are the logs of gunicorn and nginx.
I see that the favicon is missing but that only should not stop the page from being displayed right?
Gunicorn:
>>> cat /var/local/sites/hellocities/run/gunicorn.error.log
10.0.0.1 - - [28/Jan/2014:07:05:16] "GET / HTTP/1.0" 400 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:43] "GET / HTTP/1.0" 400 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
Nginx:
>>> cat /var/log/nginx/hellocities-access.log
10.0.0.1 - - [28/Jan/2014:07:05:16 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:05:20 +0000] "GET /favicon.ico HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:43 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:44 +0000] "GET /favicon.ico HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
>>> cat /var/log/nginx/hellocities-error.log
2014/01/28 07:05:20 [error] 13886#0: *1 open() "/var/local/sites/hellocities/static/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "10.0.0.200"
2014/01/28 07:09:44 [error] 13886#0: *3 open() "/var/local/sites/hellocities/static/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "10.0.0.200"
I had the same problem and adding ALLOWED_HOSTS = ("yourdomain.com",) to settings fixed it.
UPDATE: there few other possibilities:
Nginx (or whatever web server you use) doesn't pass the $host variable to the app
Host contains underscores
See details: https://blog.anvileight.com/posts/how-to-fix-bad-request-400-in-django/
As I was having the same issue (400 error code when trying to share with vagrant share), I stumble upon this question. The answer and comments are right, as the obvious solution is to set ALLOWED_HOSTS list, but I was already setting it correctly (I thought).
I can't speak for nginx as I'm running this on apache2, but here's what solved the issue:
Take a look at the ALLOWED_HOSTS doc to find what's best for your case.
With vagrant, you might find it useful to accept all the vagrantshare.com subdomain, so just add '.vagrantshare.com' (notice the dot) to the ALLOWED_HOSTS list.
Not sure if it is really necessary, but I changed the modified date of the wsgi.py file
touch wsgi.py
As I'm using apache2, I needed to restart the service.
sudo service apache2 restart
And then it worked.
I ran into this issue. It was because I forgot to add the proxy_set_header settings in the nginx config:
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
So Django didn't see the original hostname that was requested, so it didn't match with what was in ALLOWED_HOSTS. Then it gave back the 400 response.
After adding this to my nginx config (at the spot where you do the proxy_pass to Gunicorn) and then restarting nginx, it worked.
More info: https://docs.gunicorn.org/en/stable/deploy.html#nginx-configuration
I have an application running nginx,django 1.5, gunicorn.
Scenario 1
When I load my application one time - it will show the page file with an underlying 200 HTTP response.
Scenario 2
However, if I reload this same page right after, it will show my HTTP 500 Default, but custom page.
Scenario X
If I reload again, it either will show Scenario 1 or Scenario 2 upon subsequent page reloads.
I looked at my access.log for nginx and...
it is showing information like for the 500 error page:
[05/Oct/2013:03:26:07 +0000] "GET / HTTP/1.1" 500 460 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0"
[05/Oct/2013:03:21:19 +0000] "GET /static/img/templated/base/subnav_back.png HTTP/1.1" 304 0 "http://mydomain.com/static/css/base.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0"
it is showing information for a 200 HTTP response of:
[05/Oct/2013:03:18:25 +0000] "GET /static/img/templated/base/subnav_back.png HTTP/1.1" 200 7674 "http://mydomain.com/static/css/mysite_base.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0"
nginx conf looks like:
server {
server_name ec2-X-X-X-X.compute-1.amazonaws.com;
access_log /home/ubuntu/virtualenv/mysite/error/access.log;
error_log /home/ubuntu/virtualenv/mysite/error/error.log warn;
connection_pool_size 2048;
root /home/ubuntu/virtualenv/mysite/homelaunch/;
location /static/ {
alias /home/ubuntu/virtualenv/mysite/homelaunch/static/;
#alias /static/;
#root /home/ubuntu/virtualenv/mysite/homelaunch/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#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"';
}
}
error.log has:
2013/09/21 19:36:22 [error] 12680#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: XX.XXX.XX.XXX, server: ec2-x-x-x-x.compute-1.amazonaws.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "www.mysite.com", referrer: "http://www.mysite.com/dir/"