I need to setup staging for my react/laravel project.The laravel serves as api and the react is an stand alone reactjs create-react-app.
What I want to achieve is to have a domain like this
project.staging-site.com // as the main site
project.staging-site.com/backend // as the laravel site
Inside laravel, I have also a dashboard that is also built with reactjs(not by create-react-app) but by the default configuration by running the laravel built in frontend scaffolding.
So in my localhost, backend looks likes this
localhost:8000 // where the dashboard lives
localhost:8000/api/V1 //where the api routes lives(consumed by the reactjs frontend)
localhost:8000/admin/V1 // where the admin routes lives(consumed by the reactjs dashboard)
And my frontend looks like this
localhost:3000
I tried this nginx config, the frontend works, but the backend part returns 404
project.staging-site.com/backend
project.staging-site.com/backend/admin
file not found.
Here is my nginx
server {
listen 80;
server_name project.staging-site.com;
root /portal/front/build;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
location /backend/ {
root /portal/backend/current/public;
try_files $uri $uri/ /index.php?$query_string;
}
location / {
root /portal/front/build;
try_files $uri /index.html;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
And here's my route for the backend dashboard
Route::get('/', function () {
return view('index');
});
///////React
Route::group(["prefix" => "admin"], function () {
/*
|---------------------------------------------------------------------------
| React Routes
|---------------------------------------------------------------------------
*/
Route::view('/{path?}', 'index');
});
I had similar configuration in one of my projects - with front-end react app, and back-end in Laravel(no React in back-end though).
Here is what I did to deploy them on nginx.
I created separate server configs for front-end and back-end, with different listen ports for each.
Verify the route configs and ensure that the front-end hits the right back-end urls.
Also, add log configs in nginx config as given below so that you have some more information when you get some errors.
error_log /var/log/nginx/laravel-app-error.log;
access_log /var/log/nginx/laravel-app-access.log;
Hope this gives you some idea to find the root cause for the 404 error.
Related
I have a Laravel web application I'm trying to deploy to Azure Web App.
I'm using PHP 8.1 and Laravel 9, I created a resource on Azure, And deployed my web application.
I created nginx config file on /home, with this configuration (from Laravel docs):
server {
listen 80;
listen [::]:80;
server_name MY_WEBAPP_URL;
root /home/site/wwwroot/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I then set the web app's startup command to the recommended command:
cp /home/default /etc/nginx/sites-enabled/default; service nginx restart
I restarted the web app server, and tried to access it, but I get error 5XX, which says:
:( Application Error
I searched the web, but it seems like there is no solution to properly deploy PHP 8.1 application to Azure.
I tried few different nginx configuration that I saw, but none worked.
Deploying with Docker also has it's own different errors (on Azure only, works fine on local container).
I am a bit new to AWS Elastic Beanstalk I used AWS Elastic Beanstalk to create a PHP 7 deployment for a Laravel project which runs perfectly on my laptop. then uploaded my Laravel project files, edited the .env file
after uploading the Laravel project to elastic beanstalk only the homepage displays but API urls dont work and open get api pages show 404 Not Found error
Even after trying solution on this stack overflow page, the issue still persists
Please advise me on how to make other pages apart from the home page that are currently showing as 404 pages stop showing as 404 pages
=======EDITS=========
here's my EB PHP settings file
its location is
.platform/nginx/conf.d/elasticbeanstalk/laravel.conf
Content below
server {
listen 80;
server_name myrootpathapi-env.eba-ghdkmh8i.us-east-2.elasticbeanstalk.com;
root myrootpathapi-env.eba-ghdkmh8i.us-east-2.elasticbeanstalk.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
The routes and assets are loading properly on local server. However, after I deployed on production server using nginx, only the root url is working which is http://calculator.example.com. But all the assets are returning 403. Also when I try to access any routes for eg: http://calculator.example.com/page-1/ it is also returning 403.
Nginx config:
server {
listen 80;
server_name calculator.example.com;
root /var/www/html/calculator/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}
If you have directory indexing off, and is having this problem, it's probably because the try_files you are using has a directory option:
location / {
try_files $uri $uri/ /index.php?$query_string;
} ^ that is the issue
Remove it and it should work:
location / {
try_files $uri /index.php?$query_string;
}
Why this happens
TL;DR: This is caused because nginx will try to index the directory, and be blocked by itself. Throwing the error mentioned by OP.
try_files $uri $uri/ means, from the root directory, try the file pointed by the uri, if that does not exists, try a directory instead (hence the /). When nginx access a directory, it tries to index it and return the list of files inside it to the browser/client, however by default directory indexing is disabled, and so it returns the error "Nginx 403 error: directory index of [folder] is forbidden".
Directory indexing is controlled by the autoindex option: https://nginx.org/en/docs/http/ngx_http_autoindex_module.html
First, you need check if requests have been passed to app.
if 403 be returned in nginx, i think issue is user run nginx not have permission to access or execute code.
if 403 be returned in php app, please debug normally.
This issue got resolved after I removed the deny all for other location:
location ~ /.(?!well-known).* {
deny all;
}
I have been trying to host a simple Wordpress blog using NGINX as the web-server. The blog is hosted as a subdirectory under domain_name.com/blog.
The main blog opens up correctly. But when trying to open the wp-admin under domain_name.com/blog/wp-admin my browser shows ERR_TOO_MANY_REDIRECTS.
I am not sure if this is an issue with my NGINX configuration or wordpress configuration. Following is my NGINX server block:
server {
listen 80;
server_name <domain_name.com>;
root /var/www/html;
index index.php;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Wordpress is installed under the /var/www/html/blog directory. And the values for "siteurl" and "home" wp_options in the database are both pointing to domain_name.com/blog.
What would be a good way to solve this problem?
Additional notes that might be helpful:
When I try to access static files under the wp-content directory, they open without any issues. No redirection errors there.
It is usual for WordPress to redirect an http session to https whenever accessing wp-admin. This may be controlled using the FORCE_SSL_LOGIN and FORCE_SSL_ADMIN settings in wp-config.php.
When a reverse proxy is terminating SSL, the fact that the originating connection is over https must be conveyed to WordPress to avoid a redirection loop.
Your reverse proxy should be setting headers such as X-Forwarded-Proto.
You need to change your nginx configuration so that the HTTPS flag is set correctly for WordPress.
For example:
map $http_x_forwarded_proto $https_flag {
default off;
https on;
}
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php;
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param HTTPS $https_flag;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
I have tried a lot of configuration settings for nginx to work with laravel but none of them do. I keep getting a 500 error on my webpage.
example.com is currently unable to handle this request.
server {
listen _:80;
server_name example.com;
root "C:\Users\Administrator\Google Drive\projects\laravel";
index index.php index.html;
log_not_found off;
charset utf-8;
access_log logs/cdn.example.com-access.log ;
location ~ /\. {deny all;}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location / {
include mime.types;
}
location = /favicon.ico {
}
location = /robots.txt {
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9054;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am pretty brand new to laravel and sort of new to nginx, I have all my laravel files in the document root that I just moved from my http://localhost:8000 development server. How can I make it live and make it work with nginx?
I can see wrong configuration in your post. You need to properly setup you web server. You should set public directory as root directory, set write permissions on a storage folder.
So, change this:
root "C:\Users\Administrator\Google Drive\projects\laravel";
To this:
root "C:\Users\Administrator\Google Drive\projects\laravel\public";
And restart your web server.