Skip to content

Nginx Config for static file need to fix? #104

Open

Description

@ewwink

I'm using Windows, the following configuration fails if the Request URI is / and the directory /var/www/html/your-app/public exists.

# Change to your public dir
root /var/www/html/your-app/public;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ @backend;
}
# Add the ip:port of your app
location @backend {
proxy_pass 127.0.0.1:8080; // or localhost:8080;
proxy_http_version 1.1;
proxy_set_header Connection "";
}

if file index index.html or index.htm exist it will use the file else 403 Forbidden, and proxy_pass is never called

to fix I use this

# serve static file using Nginx
location ~* \.(css|js|jpg|jpeg|png|gif|ico|woff|woff2|svg)$ { 
    # remove the "$" above to match like: style.css?v=1.0
    expires 7d;
    add_header Cache-Control "public, no-transform";
    try_files $uri =404; # if file not found return 404
}

# use proxy pass including "/" only
location / {
    #try_files $uri $uri/ @backend; # disabled
    
    proxy_pass http://localhost:8080;
    proxy_http_version 1.1; 
    proxy_set_header Connection "";
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions