I am trying to serve static html files via nginx. In my nginx config, I have:
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/django/django_project/static/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin;
}
location /static {
alias /home/django/django_project/static;
}
location /minion {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
# this is what im talking about
location /html {
alias /home/django/django_project/static/html;
}
location / {
alias /home/django/django_project/static/html;
}
}
(note how the two last definitions are identical aside from the location path). Now, if I go to mywebsite.tld, I get a 403 error for some weird reason, while going to mywebsite.tld/html works exactly as it is supposed to be.
What is the explanation for this behaviour? What mistake did I make so it does this? Obviously, the file perms are ok, the index and files all exist (since /html works), it has to do with the location /, but I dont have a clue why it would... Please help me out here.
Related
I want to set my blog to point towards yourdomainanme/blog and to host a static page on the default yourdomainname.com. I changed the paths but It does not find the html file for some reason.
Error
nginx: [emerg] unknown directive "home" in /etc/nginx/sites-enabled/default:14
nginx: configuration file /etc/nginx/nginx.conf test failed
My Default File
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name yourdomain.com; # Replace with your domain
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 10G;
location / {
root var/www/;
home home.html;
}
location /blog {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
I believe you are confusing the non-existent home keyword with the index keyword, which tells NGINX the 'default' or 'index' file in the root directory.
location / {
root var/www/;
home home.html;
}
should be
location / {
root var/www/;
index home.html;
}
Setting up a site on digitalocean it's working on a default ip that comes with the setup. I've added a domain to the instance and edited the nginx conf file to add a subdomain for testing purposes. I've restarted nginx but the changes aren't taking effect. Here's the conf settings.
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _ beta.kazi-connect.com;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
I have the following nginx configuration:
server {
listen 80;
server_name default;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080/;
}
}
When the internal service at http://127.0.0.1:8080/ is not responding nginx return a 502 Bad Gatway error.
My question: how can I configure nginx to returning a static html file, e.g. /var/www/error.html, when such error occurs?
What I tried
Taking a cue from here I tried this:
server {
listen 80;
server_name default;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080/;
}
error_page 404 502 /error.html;
location = /error.html {
internal;
root /var/www;
}
}
It's working fine if the service (on the port 8080) is down, but when the service is up and I try to access the url /error.html nginx match the last location (returning me the static file) and I would like to avoid it.
Well, if you really don't want to expose error page, you could use named location.
server {
listen 80;
server_name default;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080/;
# probably you also need this
# proxy_intercept_errors on;
}
error_page 404 502 #error;
location #error {
root /var/www;
try_files /error.html /error.html;
}
}
I have a Spring Boot + MVC app up and running on my server and it's bound to http://localhost:8000.
There is an nginx proxy (or is it a reverse proxy, not sure about the name) that listens to the outside world on ports 80 and 443. The root ( / ) will resolve correctly, but anything under it will not resolve and results in a 404 error ( /someControllerName/action, /images/, /css/ ).
I have this as my configuration:
upstream jetty {
server localhost:8000;
}
server {
listen 80;
server_name domain.com;
return 301 http://www.domain.com$request_uri;
}
server {
listen 443;
server_name domain.com;
ssl_certificate /etc/nginx/ssl/ssl.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;
return 301 https://www.domain.com$request_uri;
}
server {
listen 80 default_server;
listen 443 ssl;
root /usr/share/nginx/html;
index index.html index.htm;
server_name www.domain.com localhost;
#ssl on;
ssl_certificate /etc/nginx/ssl/ssl-unified.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
proxy_pass $scheme://jetty/$request_uri;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
try_files $uri $uri/ =404;
}
}
Any help is very much appreciated.
You can't combine proxy_pass with try_files in the way that you have attempted. As the comment in your configuration describes, the try_files directive causes nginx to look for a file that matches the URI and then look for a directory that matches the URI. If it doesn't find either, it responds with a 404. You can read more about try_files in the nginx documentation.
It's not clear from your question that you need to use try_files at all so the simplest way to fix your configuration is to remove the try_files line.
I'm trying to route traffic across multiple upstream servers on nginx like so:
upstream app_a {
server unix:/tmp/app_a.sock fail_timeout=10;
# For a TCP configuration:
# server localhost:8000 fail_timeout=0;
}
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
index index.html index.htm;
server_name localhost;
root /home/ubuntu/app_a/www/staging/static;
location ~ ^/app_a/(.*)$ {
try_files $1 #proxy_to_app_a;
}
location #proxy_to_app_a {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_a;
}
Unfortunately the apps have no knowledge of full uris and expect to be sitting on root - which means i need to re-write the uri when passing to the app, which is why i thought this might work:
location ~ ^/app_a/(.*)$ {
try_files $1 #proxy_to_app_a;
}
the app works fine if location is just / (because of the aforementioned root issue), but this regex based solution doesnt seem to work. What do i need to do so the app gets / instead of app_a in the url?
Thanks
location /app_a/ {
rewrite /app_a/(.*) /$1 break;
proxy_set_header Host $http_host;
proxy_pass http://app_a;
}