Hi everyone I have changed default file of nginx where is in /etc/nginx/sites-available/default as below
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80;
location /static/ {
root /home/django/chistaa/chistaa/settings;
try_files $uri =404;
}
location / {
try_files $uri #send_to_django;
}
location #send_to_django {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://django;
}
}
but it doesnt load my static files.
What did I wrong?!
Related
server {
listen 80 default_server;
server_name http://localhost;
server_tokens off;
root /usr/share/nginx/html;
location = /home {
index index.html index.htm;
try_files $uri /index.html;
}
location = /user {
index index.html index.htm;
try_files $uri /index.html;
}
location = /login {
index index.html index.htm;
try_files $uri /index.html;
}
location = /signup {
index index.html index.htm;
try_files $uri /index.html;
}
location ^~\/verify\/\?token=([A-Za-z0-9-_]*\.[A-Za-z0-9-_]*\.[A-Za-z0-9-_]*$) {
proxy_pass 'http://localhost:8000';
}
location ~ "^\/([0-9a-zA-Z+=-]{7,})$" {
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_pass 'http://localhost:8000';
}
}
I have this nginx.conf file, I want to route /verify/?token=something.something.something(the value of token is jwt) to the backend.
But getting 404 error while trying to access the endpoint, all other routes are working properly.
I'm having problem with configuring my nginx.conf file to run django server on main domain and a WordPress site on domain.com/blog.
This is my configuration file which my WordPress dir is
/var/www/varzesh-kon/blog/:
upstream Main_Project_server {
server unix:/home/amirfarsad/django_env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 2n9l.s.serverhost.name;
client_max_body_size 4G;
access_log /home/amirfarsad/logs/nginx-access.log;
error_log /home/amirfarsad/logs/nginx-error.log;
location /static/ {
alias /home/amirfarsad/Main_Project/static/;
}
location /media/ {
alias /home/amirfarsad/Main_Project/media/;
}
location / {
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://Main_Project_server;
break;
}
}
location /blog/ {
root /var/www/varzesh-kon/blog/;
index index.php index.html index.htm;
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
root /var/www/varzesh-kon/blog/;
}
}
My django site works well but when I go to domain.com/blog, it gives me a 404 not found nginx page.
Try changing your bottom location block
location ^~ /blog {
root /var/www/varzesh-kon/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
Explanation:
In case of the root directive, full path is appended to the root including the location part
In your case:
location /blog/ {
root /var/www/varzesh-kon/blog/;
The final path that nginx will derive is going to be:
/var/www/varzesh-kon/blog/blog
That's why its showing you 404 not found page
Solution:
Either use alias instead of root
or change root path to /var/www/varzesh-kon/
location /blog/ {
root /var/www/varzesh-kon/;
...
}
location /blog/ {
alias /var/www/varzesh-kon/blog/;
...
}
for more: wiki
I work with Django and Nginx
I added the following entry to my config to restrict access to example.com/admin/
The function asks for a password, and everything works, but after that, as I get a 404 Not Found error from Nginx
Full config
upstream rates_core_server {
server unix:/webapps/example.com_app/example.com/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com www.example.com;
client_max_body_size 4G;
access_log /webapps/example.com_app/logs/nginx-access.log;
error_log /webapps/example.com_app/logs/nginx-error.log;
location /admin/ {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /static/ {
alias /webapps/example.com_app/example.com/static/;
client_max_body_size 100M;
}
location /media/ {
alias /webapps/example.com_app/example.com/static/media/;
client_max_body_size 100M;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://example.com_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/example.com_app/example.com/static/;
}
}
I do not understand what the problem is
With the current config, nginx does not know where to look for or redirect to for admin block. Can you include proxy_pass settings in your admin block as well, like this:
location /admin/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://example.com_server;
break;
}
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
This is my server block nginx config, but I don't know what is the mistake. When I hit my domain name, it gives me the "404 Not Found".
Kindly help
server {
listen 80;
listen [::]:80;
root /var/www/squareeducation.in/SE_WebApp/template/views/layouts;
index index.html index.htm index.nginx-debian.html default.hbs;
server_name www.squareeducation.in;
location / {
try_files $uri $uri.hbs/ =404;
proxy_pass 'http://127.0.0.1:3000';
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}
location ~ .*\.(img|gif|jpg|jpeg|png|bmp|swf|js|css)$ {
root /var/www/squareeducation.in/SE_WebApp/public;
try_files $uri $uri/ =404;
}
}
that means that your server cannot find where the files are located, also, I don't know your configuration, but if you are starting simple, you can try by just using:
listen 80;
listen [::]:80;
root /var/www/squareeducation.in;
index index.html index.htm index.nginx-debian.html default.hbs;
server_name www.squareeducation.in squareeducation.in;
location / {
try_files $uri $uri.hbs/ =404;
}
}
I personally always start from the simple stuff to troubleshoot. Put a basic index.html file on that route: /var/www/squareeducation.in
and see if it reads it, also apply the permissions: sudo chmod 775 /var/www/squareeducation.in
Here is a page with Nginx basic commands, it'll come handy:
https://elkepon.com/how-to-install-nginx-on-ubuntu-16-04/
Hope it helps
I have django+gunicorn+nginx running on 192.168.1.81:3000. The web app will not serve any static files; it returns a 404 error. This suggests that there is a problem with the nginx virtual server config file. I have tried several solutions offered on stack overflow with no success. What is wrong with the nginx virtual server file?
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
location /static/ {
alias /home/pi/door_site/static/;
}
location / {
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://app_server;
break;
}
}
}
In Nginx virtual server conf file inside server section you need to add to location sections for Nginx can serve files in /static/ and /media/ folders:
location /media {
alias /path/to/your/folder/media;
}
location /static {
alias /path/to/your/folder/static;
}
After that test Nginx configuration:
sudo nginx -t
and reload Nginx:
sudo nginx -s reload
(or restart - sudo /etc/init.d/nginx restart )
try this config using server root and try_files
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
root /path/to/root # place your static directories in root i.e /path/to/root/static
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
This will try to find your static file and then move onto your app server.
Make sure nginx is running as the right user to access your files and that your static files permissions are correctly set perhaps with:
chmod -R u=rwX,g=rwX,o=rX static_dir
I my case it was a permission issue on static directory and it worked after assigning proper permissions.