Nginx doesn't serve static - django

I'm running Django on Ubuntu Server 9.04.
Django works well, but nginx doesn't return static files - always 404.
Here's the config:
server {
listen 80;
server_name localhost;
#site_media - folder in uri for static files
location /static {
root /home/user/www/oil/oil_database/static_files;
autoindex on;
}
#location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
# root /home/user/www/oil/oil_database/static_files;
# access_log off;
# expires 30d;
#}
location / {
root html;
index index.html index.htm;
# host and port to fastcgi server
#fastcgi_pass 127.0.0.1:8080;
fastcgi_pass unix:/home/user/www/oil/oil_database/oil.sock;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
access_log /var/log/nginx/localhost.access_log;
error_log /var/log/nginx/localhost.error_log;
}
Nginx version is 0.6.35.
All directories exist and made 777 (debugging paranoia). The commented-out block doesn't help when I uncomment it.

How is your directory setup? Do you have a folder static in /home/user/www/oil/oil_database/static_files? In that case, the directive should look like this (note the trailing slash in /static/):
location /static/ {
autoindex on;
root /home/user/www/oil/oil_database/static_files;
}
If you want to map the path /home/user/www/oil/oil_database/static_files to the URL /static/, you have to either
rename the folder static_files to static and use this directive:
location /static/ {
autoindex on;
root /home/user/www/oil/oil_database/;
}
use an alias:
location /static/ {
autoindex on;
alias /home/user/www/oil/oil_database/static_files/;
}
See the documentation on the root and alias directives.

I have a similar config for my Django sites, but I think you want to use alias instead of root for your media. For example:
location /static {
alias /home/user/www/oil/oil_database/static_files;
}

Related

Nginx not serving files from /var/www/html after deploying Django app

I have a Django application stored at /var/www/w_gm .
Now I have used Nginx + Gunicorn to deploy it.
My default conf file at
root#dev:/etc/nginx/sites-enabled# ls
default w_gm
Default conf file :
server {
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
w_gm conf file :
server {
listen 80;
server_name 119.00.00.100;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/w_gm/w_gm/;
}
location / {
include proxy_params;
proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}
}
Now the issue is that, when am tying in my IP address i.e, it's redirecting me to the Django app and it's working perfectly fine. But my other files which are in var/www/html doesn't get served i.e if I have <ip.addr>/work1 , it gives me error.
Now if I edit the w_gm conf file and add a suffix, let's say server_name 119.00.00.100/abc; , which obviously is wrong, my /var/www/html files start working.
I need a solution where if I type in <ip.addr>/something, then it should redirect to Django app, else serve the files which are in var/www/html.
You currently have two servers, but it sounds like you only want one server.
<ip.addr>/something is ambiguous, so you need Nginx to look for files in one root and send requests to the proxy only if they are not found.
You would need to combine your two server blocks into something like this:
root /var/www/html;
location / {
try_files $uri $uri/ #proxy;
}
location ~ \.php$ {
...
}
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/w_gm/w_gm/;
}
location #proxy {
include proxy_params;
proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}
This assumes that the /static/ URI is unambiguous, and that no URIs ending with .php are sent to the proxy. See this docuemnt for more.

phpmyadmin not working on nginx web server in linux ami on amazon ec2 instance

What I am working with :
Instance - amazon EC2,
OS - linux AMI,
Web-server - nginx
I have installed phpmyadmin according to what is mentioned in the AWS documentation here
I have mysql up and running, as well as php-fpm and nginx. I also created a symlink between the directory where phpMyAdmin i.e /var/www/html/phpMyAdmin -> /usr/share/nginx/www/html
below is what I have in my nginx.conf file
server{
listen 80;
server_name localhost;
root /var/www/html/phpMyAdmin;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#location ^~ /phpMyAdmin/ {
# root /var/www/html/phpMyAdmin;
# index index.php;
# include fastcgi_params;
# fastcgi_pass unix:/var/run/php-fpm.sock;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME script/$fastcgi_script_name;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/html/phpMyAdmin;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
But none of this seems to work. I am trying from hours but unable to figure out what may be the problem due to which its not working. any help would be really great. Thanks !
You can try with below vhost config file. Error page redirection handling appears a problem with your config file.
server {
listen 80;
root /var/www/html/phpMyAdmin; # Change the PHPMyAdmin location
index index.php;
server_name default_server;# Set server name based on sub-domain
access_log /var/log/nginx/phpMyAdmin-access.log;
error_log /var/log/nginx/phpMyAdmin-error.log notice;
charset utf-8;
error_page 404 403 500 502 503 504 /index.php;
location / {
try_files $uri $uri/ /index.php?$args;
access_log off;
expires max;
}
location ~* .(jpg|jpeg|png|gif|ico|css|js|ico|swf)$ { expires 365d; access_log off; log_not_found off;}
location = /favicon.ico { log_not_found off; access_log off; allow all; }
location = /robots.txt { access_log off; log_not_found off; allow all; }
location ~ \.php$ {
expires off;
fastcgi_read_timeout 600;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/phpmyadmin.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

How to configure nginx to run Wordpress and Django

I've looked at a couple similar question, but they didn't seem to be any help. I've got a vultr.com instance running a Wordpress in the default 1 click configuration. Centos6 if that matters. This is (and I would like it to remain) in the root (www.mysite.com). I would like to have my Django app running in www.mysite.com/crossfaded/. I don't want any downtime for my current server, so there is no domain name associated with the server yet. I'm trying to do this just using the IP.
The Wordpress site is working fine.
I've been following the guide here as then tried this one, but when I navigate to http://ip.add.re.ss/crossfaded/media/apple.jpg in my browser, I get a 404 from nginx. Going /crossfaded/media/ gives me a 403 from nginx and /crossfaded/invalidpath/ gives me a 404 served by Wordpress, so something is happening with the routing. I did chmod 777 apple.jpg on the off-chance it was a permissions issue, but that didn't do anything.
I have a hunch I've got the syntax of the location block muddled, but I'm really not sure.
wordpress_http.conf
upstream php-handler-http {
server 127.0.0.1:9000;
#server unix:/var/run/php5-fpm.sock;
}
server {
listen 80 default_server;
server_name _;
#server_name wordpress.example.com;
root /var/www/html/;
index index.php;
# set max upload size
client_max_body_size 2G;
fastcgi_buffers 64 4K;
access_log /var/log/nginx/wordpress_http_access.log combined;
error_log /var/log/nginx/wordpress_http_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args ;
}
location ^~ /wp-admin/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd/wpadmin;
location ~* \.(htaccess|htpasswd) {
deny all;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-handler-http;
fastcgi_read_timeout 60s;
}
}
location ~* \.(htaccess|htpasswd) {
deny all;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-handler-http;
fastcgi_read_timeout 60s;
}
# set long EXPIRES header on static assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
access_log off;
}
location ~ /crossfaded/static/ {
alias /root/crossfaded/static/;
}
location ~ /crossfaded/media/ {
alias /root/crossfaded/media/ ;
}
}
There were two problems:
I needed a caret (^) in the location of the alias, so:
location ^~ /crossfaded/static/ {
alias /root/crossfaded/static/;
}
location ^~ /crossfaded/media/ {
alias /root/crossfaded/media/ ;
}
The server defaulted ~/ to /root/. Nginx needs r-x permissions to all directories from / to the file in question. It did not have that. Moved the project directory to /home/.

How to serve django media files via nginx ?

I'm new at Nginx, I've successfully bound my django project to Nginx. However I can't serve my static files and I guess I set my media folder's location wrongly. Here is my file tree:
root_directory
my_django_project
...
manage.py
app1
app2
media
admin
css
js
...
And my nginx.conf goes like :
server {
listen 192.168.1.9:80;
server_name localhost;
# site_media - folder in uri for static files
location /media/ {
root /home/nazmi/workspace/portal/media/;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
access_log off; # po co mi logi obrazków :)
expires 30d;
}
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8080;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
access_log /var/log/nginx/localhost.access_log main;
error_log /var/log/nginx/localhost.error_log;
}
}
When I open my admin page, all css pages give 404 error. Can you tell me that how can I set my media path correctly ?
Here's a example of how I have my nginx servers setup
server {
server_name example.com www.example.com;
location /static {
autoindex on;
alias /home/myusername/myproject/static/;
}
location /media {
autoindex on;
alias /home/myusername/myproject/media/;
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
I serve django with Gunicorn on localhost port 8000. (that's what the proxy_pass is for)
The Nginx wiki example configuration may help you too. Notice in their static file serving they specify allowed filetypes and use 'root' instead of 'alias' but they are similar.
This ServerFault question may help.
You have set this:
location /media/ {
root /home/nazmi/workspace/portal/media/;
}
This means that the location will be xxx/media/media. Nginx will go to [...]/portal/media and look for a folder called media.
It should rather be like this:
location /media/ {
root /home/nazmi/workspace/portal;
}
Also you should remove the trailing slash.
If the media file isn't serving, try to set media location as below in the conf file inside the sites-enabled directory. This works for me.
location /media {
root /home/username/projectname/;
The following code works for me:
server {
server_name example.com www.example.com;
location /static {
autoindex on;
**alias /home/myusername/myproject/;**
}
location /media {
autoindex on;
**alias /home/myusername/myproject/;**
}
location / {
proxy_pass http://127.0.0.1:8000;
}
}
I bold the different parts according to the previous answer.
location /media {
alias /home/user/django_app/media; #(locaion of your media folder)
}
Try this code in nginx config to serve static and media files for a django applications (without autoindex on settings)

Django + NGINX URL Problem

I have set up Django's FastCGI + NGINX, but it's only working for root url: any request to http://example.com/anything redirects to http://example.com. Django's internal server working fine, NGINX static file serving and FastCGI processing of the root URL is fine, error log is clear. Here is my config's server section:
server {
listen 80;
server_name example.com;
location / {
fastcgi_pass localhost:8000;
include fastcgi_params;
}
location /static/ {
alias /root/web_development/src/web_development/static/;
}
}
What am I doing wrong? Thanks in advance.
Try this configs:
server {
listen 80;
server_name example.com;
location / {
root /home/example.com/foo;
fastcgi_pass 127.0.0.1:8000;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
Make sure you've already informed nginx the port which django runs.
You may need to add this line to location /:
fastcgi_split_path_info ^()(.*)$;
from djangoandnginx