Help with static content
deeztek
nginx-forum at forum.nginx.org
Tue Nov 2 19:46:17 UTC 2021
I'm having a problem with Nginx serving static content (.css, .js, .png,
.jpg etc.).
In the below configuration, if I access the server via IP address static
content is served with no problem. However, if I switch to host name, I get
a 404 for all static content.
=== nginx.conf starts here ===
server {
#LISTEN CONFIG
listen 443 ssl;
#HSTS CONFIG UNCOMMENT BELOW TO ENABLE
add_header Strict-Transport-Security "max-age=31536000; preload";
keepalive_timeout 70;
#LOGS CONFIG
access_log /var/log/nginx/hermes_access.log;
error_log /var/log/nginx/hermes_error.log warn;
#SSL LETS ENCRYPT CERTIFICATE CONFIG
ssl_certificate /etc/letsencrypt/live/host.domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/host.domain.tld/privkey.pem;
# Turn on OCSP stapling as recommended at
# https://community.letsencrypt.org/t/integration-guide/13123
# requires nginx version >= 1.3.7
#ssl_stapling on;
#ssl_stapling_verify on;
# maintain the .well-known directory alias for Lets Encrypt Certificate
Renewals
location /.well-known {
root /var/www/html/;
}
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
#ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
proxy_max_temp_file_size 5120m;
client_max_body_size 5120m;
#set_real_ip_from 192.XXX.XXX.XXX/24;
#real_ip_header X-Forwarded-For;
#real_ip_recursive on;
include /etc/nginx/snippets/authelia.conf; # Authelia auth endpoint
index index.cfm;
location / {
set $upstream_authelia http://127.0.0.1:9091;
proxy_pass $upstream_authelia;
client_body_buffer_size 128k;
#Timeout if the real server is dead
proxy_next_upstream error timeout invalid_header http_500 http_502
http_503;
# Advanced Proxy Config
send_timeout 5m;
proxy_read_timeout 360;
proxy_send_timeout 360;
proxy_connect_timeout 360;
# Basic Proxy Config
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Uri $request_uri;
proxy_set_header X-Forwarded-Ssl on;
proxy_redirect http:// $scheme://;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_cache_bypass $cookie_session;
proxy_no_cache $cookie_session;
proxy_buffers 64 256k;
# If behind reverse proxy, forwards the correct IP
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 172.0.0.0/8;
set_real_ip_from 192.168.0.0/16;
set_real_ip_from fc00::/7;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
}
location /admin {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
include /etc/nginx/snippets/auth.conf; # Activates Authelia for specified
route/location, please ensure you have setup the domain in your
configuration.yml
}
location /users {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
=== nginx.conf ends here ===
=== authelia.conf starts here ===
# Virtual endpoint created by nginx to forward auth requests.
location /authelia {
internal;
set $upstream_authelia http://127.0.0.1:9091/api/verify;
proxy_pass_request_body off;
proxy_pass $upstream_authelia;
proxy_set_header Content-Length "";
# Timeout if the real server is dead
proxy_next_upstream error timeout invalid_header http_500 http_502
http_503;
# [REQUIRED] Needed by Authelia to check authorizations of the resource.
# Provide either X-Original-URL and X-Forwarded-Proto or
# X-Forwarded-Proto, X-Forwarded-Host and X-Forwarded-Uri or both.
# Those headers will be used by Authelia to deduce the target url of the
user.
# Basic Proxy Config
client_body_buffer_size 128k;
proxy_set_header Host $host;
proxy_set_header X-Original-URL $scheme://$http_host$request_uri;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Uri $request_uri;
proxy_set_header X-Forwarded-Ssl on;
proxy_redirect http:// $scheme://;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_cache_bypass $cookie_session;
proxy_no_cache $cookie_session;
proxy_buffers 4 32k;
# Advanced Proxy Config
send_timeout 5m;
proxy_read_timeout 240;
proxy_send_timeout 240;
proxy_connect_timeout 240;
}
=== authelia.conf ends here ===
On the below config, if I change the following line from FQDN:
error_page 401 =302 https://host.domain.tld?rd=$target_url;
to IP:
error_page 401 =302 https://192.168.1.100?rd=$target_url;
and access using IP instead of FQDN static content works.
=== auth.conf starts here ===
# Basic Authelia Config
# Send a subsequent request to Authelia to verify if the user is
authenticated
# and has the right permissions to access the resource.
auth_request /authelia;
# Set the `target_url` variable based on the request. It will be used to
build the portal
# URL with the correct redirection parameter.
auth_request_set $target_url $scheme://$http_host$request_uri;
# Set the X-Forwarded-User and X-Forwarded-Groups with the headers
# returned by Authelia for the backends which can consume them.
# This is not safe, as the backend must make sure that they come from the
# proxy. In the future, it's gonna be safe to just use OAuth.
auth_request_set $user $upstream_http_remote_user;
auth_request_set $groups $upstream_http_remote_groups;
auth_request_set $name $upstream_http_remote_name;
auth_request_set $email $upstream_http_remote_email;
proxy_set_header Remote-User $user;
proxy_set_header Remote-Groups $groups;
proxy_set_header Remote-Name $name;
proxy_set_header Remote-Email $email;
# If Authelia returns 401, then nginx redirects the user to the login
portal.
# If it returns 200, then the request pass through to the backend.
# For other type of errors, nginx will handle them as usual.
error_page 401 =302 https://host.domain.tld?rd=$target_url;
=== auth.conf ends here ===
I tried adding the following in the server block but it didn't make a
difference:
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
Posted at Nginx Forum: https://forum.nginx.org/read.php?2,292742,292742#msg-292742
More information about the nginx
mailing list