how to write a nginx conf with multilange to different backend - laravel

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)

Related

Configurate nginx to work with laravel public folder

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.

Laravel routes issue with Nginx

I've put my Laravel app files into /var/www/html/api/v2directory.
My nginx conf looks like this:
listen 80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-de$
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi$
fastcgi_param PATH_INFO $fastcgi_path_info;
}
I want to access my routes like this: [host]/api/v2/[route]
But it is throwing 404 right now except for homepage.
Please let me know what could be wrong here.
Change this line
root /var/www/html;
to
root /var/www/html/..../public;
Then restart nginx
$ sudo systemctl restart nginx

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.

configuring codeigniter on nginx

I have my codeigniter code and I have setup nginx server on localhost. But codeigniter code doesn't show up. Shows 404 not found error for all pages.
I googled around and came to know that editing /etc/nginx/sites-available/default was the key.
Here is the code I added after googling to the file, please tell me where am I wrong and what else needs to be added. I am a complete newbie to editing such files. My code for the project is in 'usr/share/nginx/html/krshop'.
Thanxx!!
server {
server_name localhost;
root /usr/share/nginx/html/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
expires 15d;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
May be these two links solves your problem,question like this are unsolved
http://www.farinspace.com/codeigniter-nginx-rewrite-rules/
https://gist.github.com/lynxluna/1050850

Codeigniter nginx 404 error

I am not sure how many times this question has been answered before, but every answer that I look at gives a different approach to solving this problem of which none of them worked. I am migrating from Apache to Nginx and am facing some serious problems with setting it up. My /etc/nginx/sites-available/default looks like this...
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www/flo2go/;
index index.php index.html index.htm;
if ($request_filename !~ (js|css|images|robots\.txt|index\.php.*) ) {
rewrite ^/(.*)$ /index.php/$1 last;
}
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ /index.php/
{
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/flo2go/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I have tried everything to make my web application work. All I keep getting is the 404:Page Not Found error. The site was working perfectly on Apache and after spending almost 3-4 hours in solving this problem I thought that it would be better to seek the advise of experts on this forum. Hope somebody can bail me out of this situation :(
Right config for your situation must look simular to this:
server {
server_name yoursitename.com;
root /usr/share/nginx/www/flo2go/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
expires 15d;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/flo2go/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
Main problem with current config is 2 .php location blocks and if which is evil.
please add following lines to your config
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
it works for me
Check the naming s of you Controller , View, Model and Database.
if you do this in your controller:
$this->load->view('main');
then the filename of your view must be same as you wrote in your controller:
main.php
nginx is more picky about case of file names than apache.
my controller was called Proto and in a file called proto.php. I renamed it to Proto.php and it started working.
Nginx is sensitive about case of controller file name.

Resources