Skip to content

Instantly share code, notes, and snippets.

@sidben
Last active May 11, 2022 16:41
Show Gist options
  • Select an option

  • Save sidben/b5a1cdf2dd0aa8e66395ec7e0db3cf02 to your computer and use it in GitHub Desktop.

Select an option

Save sidben/b5a1cdf2dd0aa8e66395ec7e0db3cf02 to your computer and use it in GitHub Desktop.
Nginx virtual host config for Laravel app with PHP 7.4
log_format custom '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent $request_time '
'"$http_referer" "$http_user_agent"';
server {
listen 80;
server_name _;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
client_max_body_size 20M;
index index.php;
error_page 404 /index.php;
access_log /var/log/nginx/access.log custom;
error_log /var/log/nginx/error.log;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.(?!well-known).* {
deny all;
}
##
# Cache Policy
##
location ~* \.(png|jpg|jpeg|gif|svg|ico)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
}
location ~* \.(js|css|pdf|html)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
##
# NGINX Status Page
##
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
access_log off;
}
##
# Gzip
##
gzip on;
gzip_vary on;
gzip_min_length 500;
gzip_proxied expired no-cache no-store private auth;
gzip_types
application/atom+xml
application/geo+json
application/javascript
application/x-javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
font/opentype
image/svg+xml
image/x-icon
text/css
text/javascript
text/plain
text/xml;
gzip_disable "MSIE [1-6]\.";
}
@sidben
Copy link
Author

sidben commented May 11, 2022

log_format was moved outside the server group to fix a config error.

nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/sites-enabled/default:21
nginx: configuration file /etc/nginx/nginx.conf test failed

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