I don't understand why nginx cannot find files when the root is under home directory, e.g. /home/ubuntu/tabs. It will give 404 error.
It works fine when the root is /var/www/tabs.
server{
listen 80;
#root /var/www/tabs;
root /home/ubuntu/tabs;
server_name 18.191.229.199;
index main.html ;
location / {
try_files $uri $uri/ =404;
}
location /folder {
try_files /folder/main1.html /folder/main2.html =404;
}
}
Related
Whenever I run server using gunicorn --bind command, all static files load without any error.
but when I try to access my domain It gives 404 error.
Django
STATIC_ROOT = '/var/www/html/static'
STATIC_URL = '/static/'
Nginx
/etc/nginx/sites-available/default
server{
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name 65.1.254.234;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
include proxy_params;
proxy_pass http://unix:/home/ubuntu/Application_Code/app.sock;
}
location /static {
autoindex on;
alias /var/www/html;
}
}
Is there any problem happening due to .env file?
I have a web app that uses Django for the backend and some frontend and ReactJS for stricly the frontend. I am setting up my Nginx configuration and I am trying to get Nginx to proxy_pass only on the "/" location and then on the rest of the locations I want it to serve the index.html file from React.
Here is my current Nginx configuration under sites-available. So the only url that does not have the prefix "/home/" or "/app/" is the homepage. I want Django to serve the homepage and then ReactJS to serve the rest.
server {
listen 80;
root /home/route/to/my/ReactJS/build;
server_name www.mydomain.com;
location = /favicon.ico { log_not_found off; }
location / {
include proxy_params;
proxy_pass http://0.0.0.0:8000;
}
location /home/ {
try_files $uri $uri/ /index.html;
}
location /app/ {
try_files $uri $uri/ /index.html;
}
}
Let me know if you need any more details or if I could clear things up.
Thanks!
Just change it to the following: (replacing the last three location blocks)
location = / {
include proxy_params;
proxy_pass http://0.0.0.0:8000;
}
location / {
try_files $uri $uri/ /index.html;
}
The location = / only matches the exact domain, everything else will be matched by location /.
I recently had a server copied over from the previous host to an AWS server. The server is Ubuntu 14.04 and the project that I am trying to run is a Laravel 5 project. I have installed nginx as the web server, here is the default file in sites available and sites enabled:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root web/project/public;
index index.php index.html index.htm;
server_name server_domain_or_IP;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have set cgi.fix_pathinfo = 0; in my php.ini file, as I was instructed to do.
There is a directory called "web" in which the project lives, and inside of the project folder lies the "public" directory.
When I try to go to the server's IP I get a 404 Not Found Error.
Any thoughts as to what may be going wrong?
I have an emberjs app and using nginx to serve it. I want to redirect to index page if the file not found. I tried using the following configuration but its showing blank page instead of index page.
server
{
listen 80 ;
server_name x.x.x.x
xyz.example.com;
root /home/ubuntu/website/dist/;
index index.html index.htm;
error_page 404 /home/ubuntu/website/dist/;
location / {
try_files $uri $uri.html $uri/ /fallback/index.html;
}
location /fallback {
root /home/ubuntu/website/dist;
}
}
I added the following block after the reference
location /{
error_page 404 = #foobar;
}
location #foobar {
rewrite .* / permanent;
}
Finally, the conf file look like this
server
{
listen 80 ;
server_name X.X.X.X
xyz.example.com;
root /home/ubuntu/website/dist/;
index index.html index.htm;
error_page 404 = #foobar;
location /{
try_files $uri $uri.html $uri/ =404;
}
location #foobar {
rewrite .* / permanent;
}
}
After update of OP
location /{
error_page 404 = #foobar;
}
Should be
location /{
try_files $uri $uri.html $uri/ #foobar;
}
In my configuration i use the host-name($host) as directory so i don't need to make a entry for each domain in the config (only for development purpose)
one issue is that try_files failed i tried different ways but all of them without success.
try_files html\$host$uri =404;
try_files $document_root\$host$uri =404;
try_files $uri =404;
try_files $document_root\$host$fastcgi_script_name =404;
server {
listen 80;
server_name default;
charset UTF-8;
#access_log logs/host.access.log main;
# redirect server error pages to the static pages
error_page 500 502 503 504 /error/50x.html;
error_page 404 = /error/404.html;
error_page 403 = /error/403.html;
location ^~ /error/ {
internal;
root html/default;
}
location / {
root html/$host;
index index.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
#try_files html\$host$uri =404;
#try_files $document_root\$host$uri =404;
#try_files $uri =404;
#try_files $document_root\$host$fastcgi_script_name =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root\$host$fastcgi_script_name;
include fastcgi_params;
}
The concept it self is correct, but you are applying it in the wrong place, you should place this as a root for the server, not in the try_files
Also I believe you should use $http_host not $host, refer to this answer to know the difference
I don't know why your paths are not absolute, they should start with /
I'll add changes to your current posted config
server {
#since you will match multiple domains I removed the server_name
listen 80 default_server;
charset UTF-8; # don't really need this but it won't hurt.
root /usr/share/nginx/html/$http_host; #assuming this path
index index.php index.html index.htm;
# redirect server error pages to the static pages
error_page 500 502 503 504 /error/50x.html;
error_page 404 = /error/404.html;
error_page 403 = /error/403.html;
location ^~ /error/ {
internal;
root /usr/share/nginx/html/default;
}
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
# these are the minimal required parameters.
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}