简体   繁体   中英

How to serve static files in nginx without hitting my gunicorn/django server?

I've have an nginx/gunicorn/django setup by following parts one and two of this guide , and it works fine. Now what I've tried to do is include my own static files that I created by building a VueJS project. My static folder now contains an admin folder (automatically put there by following the steps in part two of the guide) and a login folder. My file structure looks like this:

- myproject
    - myproject
        - __pycache__
        - __init.py__
        - settings.py
        - urls.py
        - wsgi.py
    - static
        - admin
            - css
            - fonts
            - img
            - js
        - login
            - index.html
            - static
                - css
                - img
                - js
    -db.sqlite3
    - manage.py

The most recent iteration of my nginx configuration file looks like this:

server {
    listen 8000;
    server_name 0.0.0.0;

    rewrite_log on;
    error_log /home/vagrant/debug.log debug;

    root /sync/genetesis/github/cardioflux-webapp/myproject;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static {
        alias /sync/genetesis/github/cardioflux-webapp/myproject/static;
        autoindex on;
    }

    location / {
        include proxy_params;
        proxy_pass http://0.0.0.0:8030;
    }
}

These are my results for hitting various urls: - When I hit http://0.0.0.0:8000 , I see the Django "Congratulations!" page with the rocket ship. - When I hit http://0.0.0.0:8000/admin , I see the admin page, complete with all stylings. - When I hit http://0.0.0.0:8000/login , I get a Django 404 page, which says:

Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
    1. admin/
The current path, login, didn't match any of these.

It is my understanding that nginx should only be passing requests to gunicorn/django if it cannot find static files relevant to the request, but that doesn't seem to be the case. It looks like on the admin request, it is still passing to Django, and on the login request, it isn't finding my files and is thus trying it as a GET in my Django app.

At this point, I've read so many guides, tutorials, etc. that I'm at a total loss as to what to do. I want the app to serve the static files in the "login" folder when I hit 0.0.0.0:8000/login, meaning the index.html file and the relevant css/js. I don't really need the admin part to be working, but it's all that I can get to work. I also don't really get how admin is working when it doesn't have an index.html; it must be getting something from Django, but that completely subverts how I thought this worked (get static files only from nginx if possible, otherwise other requests go to gunicorn/Django).

Can anyone explain to me how to achieve this, or point me to a resource that explains? If any more information is necessary, please let me know

You have misunderstood what that tutorial says. You've configured nginx to serve your files at the URL "yourwebsite.com/static/" only . All other paths - starting from "/", ie everything - will be routed directly to Django, because that's what you've told nginx to do . When it says "if the user is accessing a static file", it means "if the user is accessing a URL under /static/".

Now, you probably could configure nginx to look for static files under "/" first using the try_files directive. But this would be the wrong thing to do. You do not want your login page to be a static HTML page. Django should handle your entire website.

For example, you probably want your login page to look like the rest of your site, which will most likely involve things like menus and sidebars that are dynamically generated from your database via Django. And also, you will want to ensure that users who are already logged in don't get to go to the login page - Django handles that logic. And if users put in wrong username/pasword combinations, you will want to show those errors on the login form itself - again, this will be done by Django.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM