I use laravel framework for my website on nginx server
I need to apply rewrite rule on nginx
its a sample:
site.com/play/gamename/css.css --> site.com/uploads/games/gamename/css.css
or
site.com/play/gamename/js.js --> site.com/uploads/games/gamename/js.js
and I need to know How can I define rule to do it?
my website root path is
/var/www/html/siteuser/
root directories:
app
storage
public
-uploads
,...
in my old server with apache .htaccess:
RewriteCond %{REQUEST_URI} \.(jpg|png|css|js|appcache|xml|ogg|m4a)$
RewriteRule ^play/(.+) siteuser/public/uploads/games/$1 [L]
I test this but not work:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/siteuser/public;
# Add index.php to the list if you are using PHP
index index.html index.htm index.php;
server_name .site.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
if ($uri ~ ".(jpg|png|css|js|appcache|xml|ogg|m4a)$"){
set $rule_0 1$rule_0;
}
}
if ($uri ~ ".(jpg|png|css|js|appcache|xml|ogg|m4a)$"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite ^/play/(.+) /siteuser/public/uploads/games/$1 last;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
Best Answer
You should not use a
rewrite
for this at all. Use analias
edlocation
instead.For example:
This will serve only static files with the given file extensions for URLs beginning with
/play/
, from the path given in thealias
, using case insensitive matching. No PHP code can run from inside/play/
in this configuration.