I've been stuck for days just to find a way to fix this problem.
Why my Nginx can't connect to WebSocket? and always get these errors on the console:
WebSocket connection to '<URL>' failed: WebSocket is closed before the connection is established.
or
WebSocket connection to 'wss://domain.com/virtualexpo/' failed: Error during WebSocket handshake: Unexpected response code: 404
Here's my Nginx setup looks like:
upstream projectname {
server localhost:9001;
}
upstream uvsock {
server 127.0.0.1:6379;
# server unix://var/run/supervisor.sock;
}
server {
server_name domain.com www.domain.com;
sendfile on;
charset utf-8;
client_max_body_size 20M;
client_body_buffer_size 80M;
client_body_timeout 50;
location /ws/ {
proxy_pass http://uvsock;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location / {
proxy_pass http://projectname;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
...
}
location /static {
alias /home/username/server/vier/staticfiles;
}
location /media {
alias /home/username/server/vier/media;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name domain.com www.domain.com;
return 404; # managed by Certbot
}
How can I connect this Nginx with my WebSocket for use with Django Channel?
Here's my routing.py looks like:
application = ProtocolTypeRouter({
# "http": get_asgi_application(),
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
[
path('stream/<pk>/', LiveStreamConsumer),
path('discussion/global/', GlobalLiveChatConsumer),
path('somepath/', VirtualExpoConsumer)
]
)
)
)
})
The ASGI setup:
...
from django.core.asgi import get_asgi_application
from channels.layers import get_channel_layer
from channels.routing import get_default_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'
django.setup()
channel_layer = get_channel_layer()
application = get_default_application()
# application = get_asgi_application()
And my settings.py looks like this:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost", 6379)],
},
},
}
Been around looking for a way to fix them but can't find any.
The celery and Redis themselves have already run with no issue.
But it seems that the WebSocket can't connect with my Nginx config.
This is might due to your daphne server wasn't able to access the cert to redirect to wss. You may refer article here.
How can I serve media files during production?
Within nginx, I have:
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
server_name tweetz.co;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
` `# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
server{
listen 80;
server_name mywebsite.com;
return 301 https://$host$request_uri;
}
Not working. Do I need to change something else?
Below my files:
models.py
image = models.ImageField(upload_to='media',blank=True)
urls.py
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
MEDIA_ROOT = '/home/django/django_project/static-serve/media/'
MEDIA_URL = '/media/'
server {
'/home/django/django_project/static-serve/media/image.jpg'
location /media {
autoindex on;
alias /home/django/django_project/static-serve/media/;
}
}
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
template
<img src="{{ object.profile.image.url }}" class="w3-circle" style="height:106px;width:106px" alt="Avatar">
The image source in the browser is: mysite/media/media/myimage.jpg
The image is saved: home/django/django_project/media/media/myimage.jpg
Thank you for any help
You should not prepend the path with a slash, since that means you want to upload your file to a directory named media in the root of your filesystem.
You can thus for example upload it to:
image = models.ImageField(upload_to='image/',blank=True)
If you however omit the upload_to=... parameter [Django-doc], Django will simply upload this to the MEDIA_ROOT [Django-doc].
Background
When I try to access into my Django administration panel, I get Forbidden (403) CSRF verification failed. Request aborted., even if I disable the CSRF middleware. This affects all users, in different locations with different browsers. I've followed the steps to fix this error of several questions in stackoverflow, but still the same. This has been killing me for weeks.
Issue
I'm using https with Cloudflare (Free plan), but this error persist if I deactivate https. This occurs in mydomain.com/admin
Known facts
I'm using Django 2.0 and gunicorn 19.0
I've checked the cookies of my browser
I've tried removing django.middleware.csrf.CsrfViewMiddleware
This occurs just in the Django Admin dashboard, when CSRF middleware is enabled, the another POST or GET forms works fine.
I'm logged into my Django Admin dashboard, and if I logout, I can login again with no problems, but if I try from another place, or with another user, the problem occurs again.
Gunicorn config it's the default.
Configuration files
Settings.py
CSRF_TRUSTED_ORIGINS = ['.domain.com']
CSRF_COOKIE_DOMAIN = ['.domain.com', '127.0.0.1']
CSRF_COOKIE_SECURE = True
ALLOWED_HOSTS = ['127.0.0.1', 'domain.com', 'www.domain.com', '104.336.44.153', '.domain.com']
MIDDLEWARE = [
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Nginx
server {
server_name domain.com;
access_log off;
location /static {
alias /opt/myenv/myenv/static/;
}
location /descargas/dir/ {
alias /opt/myenv/myenv/dir/;
}
location / {
proxy_pass http://127.0.0.1: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"';
}
}
Please help! Thanks for reading.
The CSRF cookie is set by CsrfViewMiddleware, so you should keep it.
I would try to change your MIDDLEWARE ordering:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', # THE FIRST
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Django version is 1.10.3
Static file server is nginx
Debug mode off
The output media url has an absolute path in the server.
eg: http://127.0.0.1:8001/home/ken/hc/media/uploads/q/2017/03/01/hjqtye.png
The correct url is: http://127.0.0.1:8001/media/uploads/q/2017/03/01/hjqtye.png
How can i fix it? THX.
The relevant code as below.
settings.py
INSTALLED_APPS = [
'grappelli',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ckeditor',
'ckeditor_uploader',
'imagekit',
'debug_toolbar',
'gunicorn',
'Website',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
STATICFILES_DIRS = [(os.path.join(BASE_DIR, 'collected_static'))]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
CKEDITOR_UPLOAD_PATH = "uploads/"
#CKEDITOR_UPLOAD_PATH = os.path.join(BASE_DIR, MEDIA_ROOT, 'uploads')
Nginx conf file
server {
listen 8001;
server_name 127.0.0.1;
access_log /var/log/nginx/hc.access.log;
error_log /var/log/nginx/hc.error.log;
location / {
#uwsgi_pass 127.0.0.1:8010;
#include uwsgi_params;
proxy_pass http://127.0.0.1:8010;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /media/ {
alias /home/ken/hc/media/;
expires 1h;
access_log off;
#add Cache-Control 'public';
}
location /static/ {
alias /home/ken/hc/collected_static/;
expires 1h;
access_log off;
#include /etc/nginx/mime.types;
#add Cache-Control 'public';
}
}
I am getting a 500 error on displayable_links.js. I assume it is something with my ssl setup. Using Django 1.9.1, Python 2.7.5, PostgreSQL 9.2.14, and Mezzanine 4.1. Happens in debug and behind gunicorn. I have * for allowed hosts just to eliminate that as a possibility. It also happens directly over port 8000.
My nginc.conf:
worker_processes 1;
error_log /var/log/nginx/error.log;
error_log /var/log/nginx/error.log notice;
error_log /var/log/nginx/error.log info;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80 default_server;
root /opt/www/;
return 301 https://www2.example.com$request_uri;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}
server {
listen 443 ssl;
server_name www2.example.com;
add_header X-Frame-Options SAMEORIGIN;
server_name_in_redirect off;
root /opt/www/;
gzip off;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain
text/css
application/json
application/javascript
application/x-javascript
text/xml
application/xml
application/xml+rss
text/javascript;
ssl_certificate /etc/httpd/conf.d/2016.example.com.crt;
ssl_certificate_key /etc/httpd/conf.d/2016.example.com.key;
ssl_trusted_certificate /etc/httpd/conf.d/intermediate.crt;
ssl_dhparam /etc/httpd/conf.d/dhparams.pem;
add_header Strict-Transport-Security max-age=31536000;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_buffer_size 8k;
ssl_session_cache shared:SSL:30m;
ssl_session_timeout 30m;
ssl_stapling on;
resolver 8.8.8.8;
ssl_stapling_verify on;
ssl_prefer_server_ciphers on;
proxy_redirect off;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static/ {
autoindex on;
root /opt/www/;
}
}
}
And my settings.py:
from __future__ import absolute_import, unicode_literals
enter code here`enter code here`import os
from django.utils.translation import ugettext_lazy as _
SEARCH_MODEL_CHOICES = []
PAGE_MENU_TEMPLATES = (
(1, _("Top navigation bar"), "pages/menus/dropdown.html"),
(2, _("Left-hand tree"), "pages/menus/tree.html"),
(3, _("Footer"), "pages/menus/footer.html"),
)
PAGE_MENU_TEMPLATES_DEFAULT = (2,)
USE_MODELTRANSLATION = False
ALLOWED_HOSTS = ['*']
SERVER_EMAIL = 'server#example.com'
ADMINS = [('example', 'example#example.com'),]
TIME_ZONE = 'UTC'
USE_TZ = True
LANGUAGE_CODE = "en"
LANGUAGES = (
('en', _('English')),
)
SITE_TITLE = "example Systems"
DEBUG = False
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SITE_ID = 1
USE_I18N = False
AUTHENTICATION_BACKENDS = ("mezzanine.core.auth_backends.MezzanineBackend",)
FILE_UPLOAD_PERMISSIONS = 0o644
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "example",
"USER": "example",
"PASSWORD": "example",
"HOST": "127.0.0.1",
"PORT": "5432",
}
}
PROJECT_APP_PATH = os.path.dirname(os.path.abspath(__file__))
PROJECT_APP = os.path.basename(PROJECT_APP_PATH)
PROJECT_ROOT = BASE_DIR = os.path.dirname(PROJECT_APP_PATH)
CACHE_MIDDLEWARE_KEY_PREFIX = PROJECT_APP
STATIC_URL = "https://www2.example.com/static/"
STATIC_ROOT = "/opt/www/static/"
MEDIA_URL = STATIC_URL + "media/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, *MEDIA_URL.strip("/").split("/"))
ROOT_URLCONF = "%s.urls" % PROJECT_APP
TEMPLATES = [{u'APP_DIRS': True,
u'BACKEND': u'django.template.backends.django.DjangoTemplates',
u'DIRS': (u'/opt/www/modernbiz/theme/templates', u'/opt/www/templates'),
u'OPTIONS': {u'builtins': [u'mezzanine.template.loader_tags'],
u'context_processors': (u'django.contrib.auth.context_processors.auth',
u'django.contrib.messages.context_processors.messages',
u'django.core.context_processors.debug',
u'django.core.context_processors.i18n',
u'django.core.context_processors.static',
u'django.core.context_processors.media',
u'django.core.context_processors.request',
u'django.core.context_processors.tz',
u'mezzanine.conf.context_processors.settings',
u'mezzanine.pages.context_processors.page')}}]
INSTALLED_APPS = (
"modernbiz",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.redirects",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.sitemaps",
"django.contrib.staticfiles",
"mezzanine.boot",
"mezzanine.conf",
"mezzanine.core",
"mezzanine.generic",
"mezzanine.pages",
"mezzanine.blog",
"mezzanine.forms",
"mezzanine.galleries",
"mezzanine.twitter",
"howtos",
"mezzanine.accounts",
"mezzanine.mobile",
"polls",
)
MIDDLEWARE_CLASSES = (
"django.middleware.gzip.GZipMiddleware",
'htmlmin.middleware.MarkRequestMiddleware',
"mezzanine.core.middleware.UpdateCacheMiddleware",
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"mezzanine.core.request.CurrentRequestMiddleware",
"mezzanine.core.middleware.RedirectFallbackMiddleware",
"mezzanine.core.middleware.TemplateForDeviceMiddleware",
"mezzanine.core.middleware.TemplateForHostMiddleware",
"mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware",
"mezzanine.core.middleware.SitePermissionMiddleware",
"mezzanine.pages.middleware.PageMiddleware",
"mezzanine.core.middleware.FetchFromCacheMiddleware",
)
PACKAGE_NAME_FILEBROWSER = "filebrowser_safe"
PACKAGE_NAME_GRAPPELLI = "grappelli_safe"
OPTIONAL_APPS = (
"debug_toolbar",
"django_extensions",
"compressor",
PACKAGE_NAME_FILEBROWSER,
PACKAGE_NAME_GRAPPELLI,
)
f = os.path.join(PROJECT_APP_PATH, "local_settings.py")
if os.path.exists(f):
exec(open(f, "rb").read())
try:
from mezzanine.utils.conf import set_dynamic_settings
except ImportError:
pass
else:
set_dynamic_settings(globals())
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(asctime)s %(levelname)s [%(name)s:%(lineno)s] %(module)s %(process)d %(thread)d %(message)s'
}
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'formatter': 'verbose',
'filename': '/var/log/gunicorn/gunicorn.errors',
}
},
'loggers': {
'django.errors': {
'level': 'DEBUG',
'handlers': ['file'],
'propagate': True,
},
}
}
Edited to add the stacktrace:
Internal Server Error: /displayable_links.js
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib64/python2.7/site-packages/mezzanine/core/views.py", line 192, in displayable_links_js
for url, obj in Displayable.objects.url_map(for_user=request.user).items():
File "/usr/lib64/python2.7/site-packages/mezzanine/core/managers.py", line 366, in url_map
home = self.model(title=_("Home"))
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 408, in __init__
val = field.get_default()
File "/usr/lib/python2.7/site-packages/django/db/models/fields/related.py", line 902, in get_default
if isinstance(field_default, self.remote_field.model):
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
[01/Feb/2016 20:50:25] "GET /displayable_links.js HTTP/1.0" 500 6309
It has been fixed a few days ago after the 4.1 release. So you need to use the master branch for now until 4.1.1 or 4.2.
https://github.com/stephenmcd/mezzanine/pull/1516
I just resolved this issue with a little hack. All you need is to override the handler of URL endpoint: "^displayable_links.js$" inside your app's urls.py file. Here is what I did:
Create a custom views.py to store your custom handler function:
from django.http import (HttpResponse)
from json import dumps
def editor_link_handler(request):
links = []
sorted_links = sorted(links, key=lambda link: (link[0], link[1]['value']))
return HttpResponse(dumps([link[1] for link in sorted_links]))
Then, inside your own app's urls.py file. Here is an example code snippet which works for me now:
# urls.py // Skip to the "Important" section for my custom code
from __future__ import unicode_literals
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.views.i18n import set_language
import mezzanine
from mezzanine.core.views import direct_to_template
from mezzanine.conf import settings
from mezzanine.blog.views import blog_post_list
############################
### Important #########
##### Here is my cusom view function to handle the request
##############
from views import editor_link_handler
urlpatterns = i18n_patterns(
# Change the admin prefix here to use an alternate URL for the
# admin interface, which would be marginally more secure.
url("^admin/", include(admin.site.urls)),
)
##################
###Important:#######
##### Here comes the serious stuff (my custom code)
###############
urlpatterns += [
url("^displayable_links.js$", editor_link_handler,
name="editor_link_handler"),
]
Remember this url rule needs to be anywhere BEFORE you do:
url("^", include("mezzanine.urls")),
Because in Django URL dispatchers, ordering does matter!
If you did this as instructed, when you click the "add link" button in TinyMCE, it will be handled by your custom handler, and you should be able to add links to your blog/page without a problem.
NOTE:
This is nothing more than a hack, please comment below if this doesn't work for you or that you have a better solution.