I use Laravel with a Vue SPA. The Server is Ubuntu / Nginx. I'm not the expert on servers...
The application works well on test and stage server but not on the productive server.
On prod, the get var from the request doesn't arrive in the backend.
www.example.com?searchString=SomeText
Where i have to change the setting from the server to solve it? If someone tell me where, i can extend this question with the code.
Here the try_file line:
try_files $uri $uri/ /index.php$query_string;
Related
i have a setup which contain a domain, say:
https://my-apps-xyz.io => this is just a random/imaginary domain. An example.
I have setup in Kubernetes virtual service several apps to have the same domain but redirect in their respective deployment through the path.
So,
https://my-apps-xyz.io/app1 => app1 deployment/pod
https://my-apps-xyz.io/app2 => app2 deployment/pod
I have already successfully setup a redirection to 2 Nuxt.js apps. The third one, i want to temporarily configure for a Laravel App which is using Nginx as a reverse proxy.
How can i make/configure so the path based url successfully maps/routes in the Laravel app, is it more convenient to configure on Nginx level or Laravel app.
I tried already in Laravel
public function map()
{
Route::prefix('app3')->group(function () {
$this->mapApiRoutes();
});
//
}
But, the issue with this approach is that the bundled app.js and main.css are mapped into
https://my-apps-xyz.io/js/app.js
https://my-apps-xyz.io/css/main.css
instead of
https://my-apps-xyz.io/app3/js/app.js
https://my-apps-xyz.io/app3/css/main.css
Which ofcourse gives 404 error. How can i encompass both the web routes and api routes in Laravel to be prefixed with that path. Or better, if it's better to be done solely on nginx level, i welcome suggestions.
I already tried something like this in nginx default.conf file
location /app3/ {
root /var/www/html/app3/public;
try_files $uri $uri/ /index.php?$query_string;
}
Thanks in advance.
I am running a project which combines both nodejs and php, and the part of nodejs is a SSR nuxt (sth like next).
and requests for /api/* will be handled by php which constructed by laravel, requests for /* will be handled by nodejs which is running on 3000 port.
The key part of nginx configs is below:
location /api/ {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
How it works?
When a client come to the website by typing in address bar, the request will be handled by nodejs first.
Then, nodejs will send a request to laravel for data.
Finally, the nodejs will send the html which had already been rendered with data from laravel to the client.
So, here is the problem:
I am using a Throttle in laravel, which means laravel needs the real ip.
Every time when a new user come to the website by typing in address bar, there is a request sent from nodejs, and the laravel will considered its ip is 127.0.0.1, a 429 Too Many Requests response will be got by nodejs even the real requests are sent from different ips.
How I try to solve:
I configed the configs/trustedproxy.php:
<?php
return [
'proxies' => '127.0.0.1',
'headers' => Illuminate\Http\Request::HEADER_X_FORWARDED_ALL,
];
added proxy_set_header X-Forwarded-For $remote_addr; in nginx config:
proxy_set_header X-Forwarded-For $remote_addr;
location /api/ {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
I am sure I registered the TrustProxies::class as middleware in app\Http\Kernel.php, also restarted nginx, but it still not work. Laravel still can't get the real ip.
I am using Laravel 8.12.
How can I solve it?
I googled it but nothing helped.
Thanks a lot! I am not good at English, sorry for grammatical errors.
Finally, I work out this bug.
Strictly speaking, it's not a proxy which you can't handle it as same as proxy exactly.
The nodejs will be considered as a client by Laravel, so some javascript have to be modified:
When a client come to the website by typing in address bar, nodejs should get the real ip in server side(not the php nor client's browser), then make a request to laravel with that ip which will be added as X-Forwarded-For
so, in a SSR Nuxt project:
plugins/axios.js
import axios from 'axios';
export default ({ req }) => {
axios.interceptors.request.use(request => {
...
if (process.server) {
request.headers.common['X-Forwarded-For'] = req.headers['x-forwarded-for'];
}
return request;
});
...
}
My website uses Laravel 5.5 and PHP 7.4.
Browser should be sending URL and query parameters as shown below,
[correct url of my site]?mode=specific&number=ID
but my controller receives it like this:
[correct url of my site]?query_string=
I can confirm this by adding $request->fullUrl() into my controller.
Same code works in another environment,
the only difference is PHP = 7.0 and it does receive correct query parameters.
I'm sorry bothering SO for such a ridiculous question.
I just miss $ at nginx server configuration
try_files $uri $uri/ /index.php?query_string; <---- wrong
try_files $uri $uri/ /index.php?$query_string; <---- correct
Half a year ago my server is broke and then I quickly setuped current server and I missed this "$".
Thank you all.
I've just installed my Laravel application and it loads perfectly. I'm trying to make an api request from my machine to the application and from Vue.js application hosted on S3. Both attempts return 404, and in the browser console it says that I have a CORS problem. Ok, but I've installed fruitcake/cors, and this works locally. Also, I've seen that on AWS, when I try to reach /api/xxx/xxx, index.php is never reached. If I try without the /api prefix, it's reachable. Could that be a missconfiguration of nginx (I'm using the default configuration, and haven't changed anything)? Any help will be much appreaciated!
Like I was thinking, nginx is not configured properly by default. After I've added
location / {
try_files $uri $uri/ /index.php?$query_string;
}
everything worked.
I am leaving this topic if anyone else has the same problem.
At work we have a single staging server with a staging domain, something like https://staging.example.com. We recently decided to switch from Apache to NGINX on a new server and we're having issues with our Laravel routing.
All of our laravel apps sit in sub-directories on the staging server, like so.
https://staging.example.com/app1/public
https://staging.example.com/app2/public
I've tried configuring the NGINX conf file as specified in the Laravel docs but get a 404 when accessing any 2nd level route, i.e. https://staging.example.com/app1/public/a/b
Using something like the below config, I can access all the routes in an app.
location #laravel {
rewrite /app1/public/(.*)$ /app1/public/index.php?$1;
}
location / {
try_files $uri $uri/ #laravel;
}
However, we have many apps hosted on this server and we don't want to have to update an NGINX conf file every time we want to add an app to the server.
Is there a way of constructing a rewrite to apply to any sub-directory and keep Laravel's routing system working?
Note: I've also tried this rewrite rewrite (.*)/(.*)$ $1/index.php?$2 and that doesn't work for 2nd level routes.
Your first capture is probably too greedy, you should limit it by using:
rewrite ^(/[^/]+/[^/]+)/(.*)$ $1/index.php?$2 last;
See this useful resource on regular expressions.