Django Nginx Browser Caching Works Half-way - css

I have this in my file in my /etc/nginx/sites-available/
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/portforlio;
index index.html index.htm;
client_max_body_size 4G;
server_name khophi.co www.khophi.co;
keepalive_timeout 5;
location /media {
alias /home/portfolio/media;
}
location /static {
alias /home/portfolio/static;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
location / {
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;
}
}
With the configuration above, I'm told I've leveraged files caching when I check via https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fkhophi.co%2F&tab=desktop
Yet, when I visit the site, it doesn't load the css and I get an unstyled page.
What is missing? I have tried this and that, yet although I have also specified my root directory, it still doesn't show them when requested in the browser.
Mysteriously, pageinsights sees them and even as cached, how?
the live site is at khophi.co

The answer I figured out to be a typo.
Should be portfolio not portforlio
I added an error log to nginx and from there, I noticed it says the directory didn't exist, that prompted me to check the folder names
error_log /home/nginx/nginx_error.log warn;

Related

Nginx response error 404 when processing a specific url

I have updated my gems and I have lost my old nginx config. I´m setting a new config in nginx.conf. My new Nginx version is 1.17.3. The home page is loading and navigation from home is also right. But, if I directly type a specific url in my browser, Nginx responds a 404.
I don´t remember what I´m missing. My nginx.conf file:
events {
worker_connections 1024;
}
http {
upstream api.development {
# Path to Puma SOCK file, as defined previously
server unix:/tmp/puma.sock fail_timeout=0;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# set client body size to 10M #
client_max_body_size 10M;
gzip on;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /path-to-root/app;
index index.html index.htm;
# Proxy requests to backend API
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
rewrite ^/api(.*) /$1 break;
proxy_pass http://api.development;
}
}
include servers/*;
}
I think I have just already got it by adding: try_files $uri $uri/ /index.html =404;
My concern is that with this line it´s working on development environment, however, in production, I haven´t updated the server yet, and I don´t see that I have any try_files enabled in my config file. So´I don´t understand if this is what I should do. I´m not skilled at all in nginx config.

How to remove trailing slash after file extension using Nginx?

I have a Ghost site at example.com and www.example.com (internally, it's running on port 2368). At the same time, I have a file named form.pdf and I want to make it available (view in browser, not to ask user to download) at:
example.com/form.pdf
www.example.com/form.pdf
With configuration below, it only works if it contains trailing slash after the .pdf extension like so:
example.com/form.pdf/
www.example.com/form.pdf/
PDF file path:
/var/www/ghost/pdfs/form.pdf
Config file path:
/etc/nginx/sites-available/ghost
Config file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name 123.45.678.901 localhost example.com *.example.com;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 100M;
location "/form.pdf" {
alias /var/www/ghost/pdfs;
index form.pdf;
}
location / {
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;
}
}
When I remove the trailing slash after the .pdf extension, I got 404 error.
Question:
How to correctly configure Nginx to make it work without the trailing slash at the end?
I found the answer. This one worked for me:
location = /form.pdf {
root /var/www/ghost/pdfs;
}

Failed to load js/css from bitbucket server reverse peroxided by Nginx

I am very new with nginx and bitbucket server setup. I have an nginx server setup in front of bitbucket instance (running on port 7990). Below is my configuration of /etc/nginx/sites-available/default (not /etc/nginx/nginx.conf)
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index 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;
}
location /bitbucket/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:7990/;
sendfile off;
expires 0;
add_header Cache-Control private;
add_header Cache-Control no-store;
add_header Cache-Control no-cache;
}
}
Now if I hit
http://my_ip/bitbucket
it forwards to
http://my_ip/bitbucket/setup
page, but fails to load css and js files.
When I see the html source of setup page, it includes some js/css files' absolute paths while importing (e.g. link type="text/css" rel="stylesheet" href="/s/277823dc49fd32854c324f87e345f7f6-CDN/-196139424/aef68c7/1/1b615eee93091f36273c8450f7d72556/_/download/contextbatch/css/_super/batch.css"). This seems like nginx is looking for the css files in "s" folder under its own directory.
I also checked that
http://my_ip/bitbucket/s/277823dc49fd32854c324f87e345f7f6-CDN/-196139424/aef68c7/1/1b615eee93091f36273c8450f7d72556/_/download/contextbatch/css/_super/batch.css
Url returns the page in browser.
Please let me know how to load css/js files. I also want to know why the css url is so ugly(it seems that it includes some random numbers).

Nginx sub-path shows 404 error

I am trying to setup nginx to work with a subpath but I am receiving 404 error.I am using default location and html file.
Nginx config is the following:
server {
listen 80;
listen[::]:80 ipv6only=on;
server_name localhost;
## serving gogs
location / {
proxy_pass http://localhost:3000;
}
## serving laravel-based web app
location /yt/ {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
The firewall (ufw) is disabled and all ports are open via mikrotik.Everything is fine when open the / path but when I go to /yt/ I get a 404 error.
Not sure if configuration is correct, when i compared with my local simple setup i found it little different . Please try this one if it works.
server {
listen 80;
listen[::]:80 ipv6only=on;
server_name localhost;
## serving gogs
location /yt/ {
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_set_header Host $host;
}
}

Reverse proxy a URI to a Vagrant box

I have a Wordpress Vagrant box that I access through the URL localhost:9001 (forwarded port.).
I am currently trying to make it accessible through the URL "molecare.dev".
I have created the line in the hosts file that catches that URL and points it to my localhost (this is working because I can see the nGinx splash page) but I am having trouble catching this URL in the server block and proxy_pass'ing this to the URL(localhost:9001).
Here is my /etc/nginx/sites-available/default file`server_name molecare.dev;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
I have written the following code which I think should do it but I don't know where to place it and if it is correct?
server {
listen 80;
server_name molecare.dev;
location / {
proxy_pass localhost:9001;
}
}
Can anyone see if this is correct and if so where I put this?
Thanks!
In the folder /etc/nginx/sites-available/ create a config file for your site, say 'molecare.dev.conf'.
Modify the block you wrote to the following and put that in the new file and save it:
server {
listen 80;
server_name molecare.dev;
location / {
proxy_pass http://127.0.0.1:9001;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Then create a symlink:
ln -s /etc/nginx/sites-available/molecare.dev.conf /etc/nginx/sites-enabled/molecare.dev.conf
Reload the nginx configuration:
service nginx reload

Resources