QueraTeam / django-nextjs

Next.js integration for Django projects
MIT License
348 stars 18 forks source link

Having issues with NGINX #15

Closed kilam2468 closed 1 year ago

kilam2468 commented 1 year ago

Not gonna lie, im pretty new to NGINX, even after reading the documentation and the production tab for nginx I still don't understand how to configure it well to work seamlessly with this package. I have it working well in development, just the process of moving it to production with nginx's configuration is where im having issues. Any Help or examples would be greatly appreciated

sebherrerabe commented 1 year ago

I'm working in a boilerplate repo to deploy "django-nextjs" projects. Soon, I'll make a tutorial more detailed but for now you can check this example https://github.com/sebherrerabe/django-nextjs-prod .

mjnaderi commented 1 year ago

Here is another config as a simple starting point for production. This is for demonstration and is not complete at all. I assume uWSGI is used as application server for serving django requests, and PM2 is used for running next.js server.

/etc/nginx/nginx.conf:

http {

    # ... other config ...

    # gzip settings
    gzip on;
    gzip_comp_level 2;
    gzip_proxied any;
    # Don't compress anything that's already small and unlikely to shrink much if at all
    # The default is 20 bytes, which is bad as that usually leads to larger files after gzipping
    gzip_min_length 1000;  # 1000 bytes
    gzip_types
        text/plain
        text/html
        text/css
        text/javascript
        application/x-javascript
        application/javascript
        application/json
        text/xml
        application/xml
        application/rss+xml
        application/atom+xml
        application/xhtml+xml
        image/svg+xml;

    # ... other config ...

    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-enabled/my_django_nextjs_website:

upstream django {
    server unix:/path/to/uwsgi.sock;  # unix socket file from uwsgi
}

upstream nextjs {
    server 127.0.0.1:3000;  # preferrably served by a node.js process manager (like PM2)
}

server {
    listen 443 ssl http2;
    server_name example.com;

    # SSL config here...

    # max upload size
    client_max_body_size 200M;

    # django static files
    location /static/ {
        alias /path/to/django/static/;
        expires max;
        add_header Cache-Control "public";
    }

    # NextJS
    location /_next/static/ {
        alias /path/to/next/project/.next/static/;
        expires max;
        add_header Cache-Control "public";
    }
    location /_next/ {
        proxy_set_header  x-real-ip  $remote_addr;
        proxy_pass  http://nextjs;
    }
    location /next/ {
        alias /path/to/next/project/public/next/;
        expires max;
        add_header Cache-Control "public";
    }

    location / {
        uwsgi_pass  django;
        include /path/to/uwsgi_params;
    }
}