Configurate nginx to work with laravel public folder - laravel

Here is my nginx.conf file :
The config file i've made not working and it doesn't go to http://localhost/api
my system is manjaro
....
http {
....
server {
....
}
include /etc/nginx/sites-enabled/*;
}
and my laravel config file is :
server {
root /srv/tamsam/public;
index index.php index.html index.htm;
gzip on;
gzip_types text/plain application/xml text/css application/javascript;
gzip_min_length 1000;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location /storage {
alias /srv/tamsam/public;
try_files $uri $uri/ #laravelapi;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
location /api {
alias /srv/tamsam/public;
try_files $uri $uri/ #laravelapi;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
location #laravelapi {
rewrite /api/(.*)?$ /api/index.php?$is_args$args last;
}
}
but the http://localhost/api give me the error 404 Not Found
I appreciate your help thanks

Looks like your #laravelapi locations configuration is not correct.
location #laravelapi {
rewrite /api/(.*)$ /api/index.php last;
}
Please also check your php configuration
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
The fastcgi_param SCRIPT_FILENAME $request_filename; is important to make it work.

Related

how to write a nginx conf with multilange to different backend

I want to deploy a multilanguage site.
like default aaa.com is English, aaa.com/jp and aaa.com/es .
I don't want to write code to implement multi-language functions,because it is someone else's code...too hard to read.
backend is a laravel app in docker.
so I create 3 container for each language.
But Nginx is not working as I thought,
Hope someone can help to correct the configuration file.
server {
gzip on;
listen 443 ssl;
server_name www.aaa.com aaa.com;
root /var/www/html/public;
location = /50x.html {
root /usr/share/nginx/html;
}
location es/ {
root /var/www/html.es/public;
try_files $uri $uri/ /index.php$is_args$query_string;
}
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
rewrite ^/es/(.*)$ /$1 break;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass es-app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
I am not proficient in the configuration syntax of nginx, and I have not achieved the desired function after trying various writing methods.
When using Nginx you have to be specially careful with the slashes, because it's very sensitive.
You cant try with location /es/ instead of only location es/ (Note the slash at the start)

Updating nginx config while attempting to invert directory structure

I am trying to flip the directory structure of a PHP app that contains a Laravel sub-app that currently has the following directory structure:
/-
/directory1-
script1.php
/laravel-
/public-
/css-
some-file.css
... other laravel files ...
What I'm trying to do is now flip the "legacy" code in the parent directory into the Laravel application so that the directory structure now looks like this:
/-
/legacy-
/directory1-
script1.php
/public-
/css-
some-file.css
... other laravel files ...
I'm running into difficulty with the nginx configuration, I am able to successfully run the /legacy scripts and load the static public files in Laravel (such as some-file.css above), but none of my Laravel routes are working correctly. This is complicated by the fact that I don't want any of the legacy paths to change (I still want / to go to the now /legacy/index.php) and I want the Laravel app to be prefixed by /laravel (since I have front-end code calling an API at /laravel/api/v1 that I don't want to update just yet).
Here's what I have for the nginx config for the two different locations:
server {
# ...
root /var/www/legacy;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ $uri.html $uri.php?$query_string;
}
location /laravel {
fastcgi_index index.php;
include fastcgi_params;
rewrite ^/laravel/public/(.*) /../public/$1;
try_files $uri /../public/index.php?$query_string;
}
}
The error I am getting from nginx is: FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, which I think is talking about the SCRIPT_FILENAME FastCGI param. I tried adding a named location for this, similar to the approach in this answer:
location / {
try_files $uri $uri/ $uri.html $uri.php?$query_string #laravel;
}
location #laravel {
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME /var/www/public/index.php?$query_string;
include fastcgi_params;
}
But this has the same effect. If someone has a solution that will be able to route requests from both /laravel/api/v1/.* and /api/v1/.* to the Laravel app that would also be appreciated.
Assuming the parent directory is www, I think the below config (from this post) will work:
server {
# ...
root /var/www/legacy;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ $uri.html $uri.php?$query_string;
}
location /laravel {
alias /var/www/public;
try_files $uri $uri/ #laravel;
location ~ \.php {
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass php-upstream;
}
}
location #laravel {
rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;
}
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-upstream;
}
}
To deal with duplicate fastcgi_* declarations, you can either declare them in the server block or copy them in a file and include it, this article explains how inheritance in Nginx works.

I get a 404 on a laravel app running on Nginx

I have this nginx config.
server {
listen 80;
listen [::]:80;
server_name apps.myapp.com;
root /var/www/apps.myapp.com/public;
index index.php;
location / {
try_files $uri $uri/ index.php?$query_string;
}
}
I have this route:
Route::get('/lead', 'LeadsController#index' );
When I access to http://apps.myapp.com/lead, I get 404.
You can run nginx -t to check if there any error in the file configuration, in addition to that you need to add fastcgi_pass
Below is nginx configuration from my production server
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public/;
index index.php index.html index.htm;
server_name xxx.xxx.xxx.xxx;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You can read this tutorial is very helpful:
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04
This is the configuration I use for my Laravel app.
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;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$query_string;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Nginx disable url rewrite for specific path / url

I have the following nginx configuration for the url rewrite
location / { ##merge
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location /devtools/phpmyadmin/ { ##merge
root /var/www/domain.tld/web;
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/lib/php5-fpm/web2.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
in the /var/www/domain.tld/web/ directory have an /api/ directory and I want disable for the url rewrite for this. So if I woud like access from url with index.php: http://domain.tld/api/index.php/function/method
How can I modify the nginx config file?
Edit:
I try the following rewrite, but not working:
location = /api {
rewrite ^ /api/index.php;
}
I confess that I do not understand your configuration file. Generally, you would define a root to be inherited by all (or most) of your location blocks. And usually a separate location ~ \.php$ block to handle off-loading .php scripts to a PHP processor via FastCGI.
Ignoring your location /devtools/phpmyadmin/ block for the moment, a typical implementation would look like this:
root /var/www/domain.tld/web;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/lib/php5-fpm/web2.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
The nginx directives are documented here.
If you want URIs that begin with /api to use a different controller, you could add:
location /api {
try_files $uri $uri/ /api/index.php;
}
I cannot see the purpose of the location /devtools/phpmyadmin/ block, as it does not seem to add any more functionality.

Nested Codeigniter apps not working in NGINX

We've setup 2 CodeIgniter apps which has its own complete code bases on its own directories as in this example. Plus we also have wordpress blog on the same public_html directory.
public_html/HOME_APP
public_html/admin_tool/ADMIN_TOOL_CODES
public_html/blog/WORDPRESS_CODES
HOME_APP codes (CodeIgniter) and our WORDPRESS works fine. But the admin_tool (CodeIgniter) doesn't work. We can access only the http://example.com/admin_tool/index.php but not any inside controller pages. When accessing its show 404 error page. And it looks like the routes are handled via APP1
The nginx rules are as follows. Appreciate if anyone can help us to fix the issue with admin_tool
server{
listen 80;
root /home/ubuntu/websites/example.com/public_html;
index index.html index.htm index.php;
server_name example.com;
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
index index.php;
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ^~/admin_tool/ {
root /home/ubuntu/websites/example.com/public_html/admin_tool;
index index.php;
try_files $uri $uri/ /index.php$args;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
The following should do the trick for all of your installations:
# We define the index directory at the outermost level and therefore
# only once for all servers. Also note that we use the PHP file first
# because all main directories are handled by PHP scripts and this will
# give us best performance.
index index.php index.html index.htm;
server {
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
# 80 is default!
#listen 80;
root /home/ubuntu/websites/example.com/public_html;
server_name example.com;
location / {
# Don't allow access to the logs directory.
location ~* ^/logs {
return 404;
}
# Don't allow access to PHP files.
location ~* \.php$ {
return 404;
}
# Handle static files.
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
# Directly return if the requested URI is a real file.
try_files $uri $uri/ index.php =404;
}
# Codeigniter and WordPress will always handle everything with their
# index.php script, therefore we only need to catch that case.
location = /index.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
I had the same problem while I am using codeigniter in subfolder(dashboard).
All non-wordpress requests are not being entertained but remain on wordpress main page.
I fixed my issue by following lines in my configuration file /etc/nginx/sites-available/{my-site-name}
location /dashboard {
try_files $uri $uri/ /dashboard/index.php;
}
location /index.php {
fastcgi_pass unix:/usr/sbin/php5-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Resources