I'd like to add a Laravel-instance to an existing website.
Based on the URL, the main website or the Laravel-instance should take the request. The URL-Structure is as follows:
Main Application (e.g.)
Laravel blog inside the page (e.g.)
- https://www.mypage.com/blog
- https://www.mypage.com/blog/post
- https://www.mypage.com/blog/create
- https://www.mypage.com/blog/{date}/{title}
and also shop (e.g.):
So I thought, I'd add 2 locations inside the existing website
- location ^~ /shop { alias ... }
- location ^~ /blog { alias ... }
However, when the request is passed on to Laravel, the first part of the URI is missing as nginx won't forward it after matching. So for the location blog the URI becomes /post instead of /blog/post and for the location shop the URL becomes /{itemname} instead of /shop/{itemname}. This results in 404s for every page, as Laravel can't find them based on the route.
How can I tell nginx to forward the complete URI including the matched location-name?
Here's my current nginx configuration:
server {
listen 80 default_server;
server_name mypage.com;
return 301 https://www.mypage.com$request_uri;
}
server {
listen 443;
server_name mypage.com;
ssl_certificate /etc/letsencrypt/live/www.mypage.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.mypage.com/privkey.pem;
return 301 https://www.mypage.com$request_uri;
}
# Default server configuration
server {
listen 443 ssl http2 default_server;
server_name www.mypage.com;
ssl_certificate /etc/letsencrypt/live/www.mypage.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.mypage.com/privkey.pem;
# add Strict-Transport-Security to prevent man in the middle attacks
add_header Strict-Transport-Security "max-age=31536000";
root /var/www/Domains/mypage.com/_www/web;
error_log /var/www/Domains/mypage.com/_www/log/error.log;
# remove tailing slash (as backend won't work otherwise)
rewrite ^/(.*)/$ /$1 permanent;
# Media: images, icons, video, audio
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$ {
expires 1M;
gzip_static on;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1M;
gzip_static on;
access_log off;
add_header Cache-Control "public";
}
# strip app.php/ prefix if it is present
rewrite ^/index\.php/?(.*)$ /$1 permanent;
location / {
try_files $uri @rewriteapp;
}
## LARAVEL SUBSYSTEM ==>
# Blog
location ^~ /blog {
alias /var/www/Domains/mypage.com/laravel/public;
index index.php;
try_files $uri $uri/ @blog;
access_log /var/www/Domains/mypage.com/livesystem/storage/logs/access.log;
error_log /var/www/Domains/mypage.com/livesystem/storage/logs/error.log;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
location @blog {
rewrite /blog/(.*)$ /index.php?/blog/$1 last;
}
# Shop
location ^~ /shop {
alias /var/www/Domains/mypage.com/laravel/public;
index index.php;
try_files $uri $uri/ @shop;
access_log /var/www/Domains/mypage.com/livesystem/storage/logs/access.log;
error_log /var/www/Domains/mypage.com/livesystem/storage/logs/error.log;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
location @shop {
rewrite /shop/(.*)$ /index.php?/shop/$1 last;
}
## <== /LARAVEL SUBSYSTEM
location @rewriteapp {
rewrite ^(.*)$ /index.php/$1 last;
}
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(index|backend)\.php(/|$) {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire