getting a simple php redirect working with nginx
@!
Hubzilla Support Forum Calling all nginx and php gurus. I am helping @
emanuel @ la bonne heure move labonneheure to a new server. Part of this transition is moving from apache to nginx.
Previously there were a number of shortcuts that were as follows:
/var/www/hubzilla/foo/index.php
The contents of which were:
<?php
header('Location:https://example.com/channel/foo');
?>
Which meant that example.com/foo redirected to example.com/channel/foo. However we are having difficulty making this work in nginx. We have written a location block for the nginx config as follows:
location ~* /(foo|bar|baz)/ {
try_files $uri =404;
}
But wherever it is placed in the config it isn't picked up. We would appreciate pointers on what is wrong with the block and/or where it goes is the nginx config. Thanks to @
Harald Eilertsen for his help so far.
The full nginx config (stripped of comments) is here:
server {
listen 80;
server_name example.com;
index index.php;
root /var/www/hubzilla;
rewrite ^ https://example.com$request_uri? permanent;
}
server {
listen 443 ssl;
server_name example.com;
ssl on;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
fastcgi_param HTTPS on;
index index.php;
charset utf-8;
root /var/www/hubzilla;
access_log /var/log/nginx/hubzilla.log;
client_max_body_size 20m;
client_body_buffer_size 128k;
location / {
if ($is_args != "") {
rewrite ^/(.*) /index.php?q=$uri&$args last;
}
rewrite ^/(.*) /index.php?q=$uri last;
}
location ^~ /.well-known/ {
allow all;
rewrite ^/(.*) /index.php?q=$uri&$args last;
}
location ~* \.(jpg|jpeg|gif|png|ico|css|js|htm|html|map|ttf|woff|woff2|svg)$ {
expires 30d;
try_files $uri /index.php?q=$uri&$args;
}
location ~* \.(tpl|md|tgz|log|out)$ {
deny all;
}
location ~* \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\. {
deny all;
}
location ~ /store {
deny all;
}
}