nginx one domain and multiple projects setting alias - laravel

I want to use one domain to map different project.
mydomain.com.tw => /var/www/project1/public
mydomain.com.tw/test => /var/www/project2
but mydomain.com.tw/project2/detail always direct to mydomain.com.tw project route.
project1 => laravel
project2 => vue and using vue-route
listen 80;
listen [::]:80;
server_name mydomain.dev;
index index.php index.html index.htm;
root /var/www/project1/public;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /lottery {
alias /var/www/project2/dist;
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}

I solved this question.
Solution:
https://gist.github.com/Maras0830/dc6f627eba005bdfc6b741f7f2ea3178

Related

Nginx server laravel route directory 404 error on Windows

I'm getting a 404 every time I try to open a page other than the welcome page. I've read all the posts here on how to fix the problem, but none of the solutions worked for me.
I'm on Windows 10 and use WPN-XM for the Nginx server.
My laravel project is installed in C:/server/www/myapp
My configuration looks as follows:
server {
listen 127.0.0.1:80;
server_name localhost;
root www;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_pool;
fastcgi_index index.php;
#fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
#fastcgi_param PHP_FCGI_CHILDREN 100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
# mitigate namespace conflict, see httpoxy.org
fastcgi_param HTTP_PROXY "";
include fastcgi_params;
}
I tried replacing root www with "C:/server/www/myapp/public" and a couple of other options.
I used try_files $uri $uri/ /index.php$is_args$args instead of try_files $uri $uri/ /index.php?$query_string
Any help will be appreciated.

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 config for two Laravel projects in different location

I'm trying to run two Laravel projects in a different location without using domain name here my config file.
server {
listen 80 default_server;
server_name ip ;
index index.php index.html index.htm;
location /project1 {
root /var/www/project1/public;
try_files $uri $uri/ /index.php?$query_string;
}
location /project2 {
root /var/project2/public;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
That returns 404 for both and from the error log return /(nginx root)/favicon.ico failed to open.
I solved using another port
some thing like that
server {
listen 85 ;
listen [::]:85 ;
server_name ip:85 ;
root /var/www/html/projectfolder1;
}
server {
listen 90 ;
listen [::]:90 ;
server_name ip:90 ;
root /var/www/projectfolder2;
}
hope that help some one

Laravel nginx default file

I try to configure my laravel installation on a digital ocean instance with nginx running.
The default config looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/mfserver/public;
index index.php index.html index.htm;
server_name IPADDRESS;
location / {
try_files $uri $uri/ /index.php$is_args&args =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mfserver/public;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php$is_args&args =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;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
}
The problem is, that when I call a route like /v1/aeds I get an error 404. Is the config not setup properly?
The route file:
Route::group(['domain' => 'SERVERIP', 'namespace' => 'API'], function() {
Route::group(['prefix' => 'v1', 'namespace' => 'v1'], function() {
// AED ROUTES
Route::get('/aeds', 'AED\APIAEDController#index');
Route::post('/aeds', 'AED\APIAEDController#store');
Route::get('/aeds/{aeds}', 'AED\APIAEDController#show');
}
Try replacing this:
location / {
try_files $uri $uri/ /index.php$is_args&args =404;
}
With this:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;

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