Custom nginx configuration for Laravel in AWS Elastic Beanstalk - laravel

Hi I am using AWS elastic beanstalk for my Laravel application
When I first uploaded my application it is not showing the login page, 404 error so I tried with app_url/index.php/login and it worked
This is how I figure out I need to add this to my Nginx configuration, I manually added this, ssh into ec2 instance, and worked
location / {
try_files $uri $uri/ /index.php?$query_string;
}
but every time I deploy my new code it replaces my manual code
I also read https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-tomcat-proxy.html?icmpid=docs_elasticbeanstalk_console this doc and created this folder structure in my application root folder
.ebextensions
nginx
conf.d
elasticbeanstalk
my-server-conf.conf
Seems not working for me
Please let me know what should I put In my-server-conf.conf file and what should I do so it works for every deploy

You should be using .platform, not .ebextensions as explained in aws docs. For example:
.platform/nginx/conf.d/elasticbeanstalk/laravel.conf
location / {
try_files $uri $uri/ /index.php?$query_string;
}

Related

Nginx proxy return 404 with Laravel and Vue

i have a problem with nginx and laravel/vue app, hope yours help
I have two web app call Admin and System.
Admin use Vuejs call api to System using Nginx proxy (image bellow)
server {
listen 80;
server_name admin.abc.com;
location / {
root /usr/share/nginx/html/onestudy-vuejs/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass https://system.abc.com;
}
location ~ /\.ht {
deny all;
}
}
After some day not access to web, i have error such as when access api from admin domain (example: https://admin.abc.com/api/user_info)
{"error":{"name":"Error","status":404,"message":"There is no method to handle ","statusCode":404}}
every request 3 times, 2 times ok and 1 time i see this error
but if access direct from system admin, everything will be still ok
After this, i just restart nginx, i don't see this error??? :( don't understand, don't know exactly what the cause is ##
Note: My project has two env, staging and prod, but only prod has error (same config nginx between two env)
Some info of project
Tech stack: Laravel, Vue
Webserver: Nginx
Cloud: AWS (EC2 + ALB)
I see this topic nginx proxy_pass 404 error, don't understand why but it's not for my problem
Glad for your support.
If u need info of system for anwser, please request bellow. Tks u

How do you use basic_auth for all but one route using Laravel Forge

I am using the in-built HTTP basic access authentication for Laravel Forge.
I am currently using basic_auth for all routes.
I would like to exclude one route (basic_auth off).
I tried to customise the file, example: /etc/nginx/forge-conf/.../server/.htpasswd-{ruleId}
I added the following additional code:
location = /example {
basic_auth off;
}
I also tried:
location /example {
basic_auth off;
}
I restarted nginx. I restarted the server. Same result: no change. The route /example was not excluded from basic_auth.
I was unable to find a solution using the in-built "Security" tool for basic authentication provided by Laravel Forge.
I was able to solve the problem by updating the nginx file for the site without using the aforementioned "Security" tool.
Replace www.example.com with your site address on Laravel Forge.
Replace /example with the path that you would like to exclude.
location / {
auth_basic "Restricted Access";
auth_basic_user_file /home/forge/www.example.com/.htpasswd;
try_files $uri $uri/ /index.php?$query_string;
}
location /example {
auth_basic off;
try_files $uri $uri/ /index.php?$query_string;
}
I created a .htpasswd file (search Google to find out how to generate your own with a username and password). I uploaded it to the root of my project directory, at the same level as the .env file.

Insert new Nginx conf in Elastic Beanstalk

I am migrating to Elastic Beanstalk and was not able to make changes to the nginx file, or even increment a new file. All configurations per AWS documentation did not work. So I did it as follows:
/.ebextesions -> 01_laravel_nginx.config:
files:
"/opt/elasticbeanstalk/support/conf/laravel.conf":
mode: "000644"
owner: root
group: root
content: |
location / {
try_files $ uri $ uri / /index.php?$query_string;
gzip_static on;
}
/.ebextesions -> 02_install_nginx.config:
container_commands:
01_runmyshellscript:
command: "sudo ln -s /opt/elasticbeanstalk/support/conf/laravel.conf /var/proxy/staging/nginx/conf.d/elasticbeanstalk"
After performing this configuration, the same information as in the /var/proxy/staging/nginx/conf.d/elasticbeanstalk directory will be in the /etc/nginx/conf.d/elasticbeanstalk directory.
Is there any better way?
I was just trying to add one more conf, instead of changing what already existed by default. I tried to perform this configuration as follows:
.platform / nginx / conf.d / laraval.conf
But I didn't create. have to create all the files?
.platform / nginx / conf.d / laraval.conf
.platform / nginx / nginx.conf
The proper way to setup nginx on EB environments using Amazon Linux 2 (AL2) is through .platform/nginx/conf.d/ as shown in the docs. Or if you want to fully overwrite the main nginx.conf you should provide its custom version using .platform/nginx/nginx.conf.

AWS Elasticbeanstalk overriding Nginx config using .platform is not working

I am deploying my Laravel application to AWS ElasticBeanstalk. I have deployed it. Now, I am trying to override "/etc/nginx/conf.d/elasticbeanstalk/php.conf" file using .platform folder.
I created .platform/etc/nginx/conf.d/elasticbeanstalk/php.conf file right inside the project's root folder. Then I put in the configuration content.
Then I deploy my application executing "be deploy" command. But the Nginx config file is not overridden. What is wrong with my config and how can I get it working?
I tried using .ebextensions too creating a config file with the following content. The file is not just created.
files:
/etc/nginx/conf.d/elasticbeanstalk/laravel.conf:
mode: "000755"
owner: root
group: root
content: |
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
The nginx config file you are trying to set is used in Amazon Linux 1 (AL1).
For AL2, the nginx config files should be provided (aws docs) using:
.platform/nginx/conf.d/
or if you want to overwrite main nginx config file, use
.platform/nginx/nginx.conf
Thus, you can try the following file .platform/nginx/conf.d/laravel.conf with content of:
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
If it does not work, you can inspect nginx logs in /var/log or nginx config folder to check if the file is correctly placed in nginx config folder.
Solution I used
I used this and it worked.
.platform/nginx/conf.d/elasticbeanstalk/laravel.conf

How to change default Nginx setting on Homestead Laravel Virtualbox VM

I merely followed the default Laravel Homestead setup here using VirtualBox. Working great.
But I need an additional Nginx rewrite setting in my apps vhost file on the VM, something like;
location / {
if ($request_method !~ "(POST)"){
rewrite ....
}
try_files $uri $uri/ /index.php?$query_string;
}
I can add that manually to the vhost file on the VM, but it's removed every time I provision my box.
How do I make that setting automatically applied when provision my vagrant VM box.
Is there a after provision hook, so I can run a script, or is there another easy solution available?
Easiest way to achieve this is editing scripts/serve.sh file, which contains server block template in block variable.

Resources