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.
Related
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;
}
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
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.
My nginx site root points to a symlink. If I alter the symlink (aka deploy a new version of the website) the old version of the php script keeps showing up.
That smells like cache, or a bug.
First it looked like Nginx was caching the symlinked dir, but reloading/restarting/killing and starting nginx didn't fix it, so I restarted php5-fpm - this fix my issue.
But I dont want to restart nginx and/or php5-fpm after a deploy - I want to know why there is such a cache (or bug), and why it didn't work properly.
Usefull information:
OS: Ubuntu 13.10 (GNU/Linux 3.8.0-19-generic x86_64)
Nginx: via ppa:nginx/stable
PHP: via ppa:ondrej/php5 (php5-fpm)
Nginx site config:
root /home/rob/sandbox/deploy/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php;
}
Nginx server config (partly, rest is default):
http {
sendfile off;
upstream php {
server unix:/var/run/php5-fpm.sock;
}
}
Tree for /home/rob/sandbox:
├── deploy -> web2
├── web1
│ └── public
│ └── index.php (echo ONE)
└── web2
└── public
└── index.php (echo TWO)
request: http://localhost/index.php
expected response: TWO
actual response: ONE
Part of the output from realpath_cache_get()
[/home/rob/sandbox/deploy/public/index.php] => Array (
[key] => 1.4538996210143E+19
[is_dir] =>
[realpath] => /home/rob/sandbox/web2/public/index.php
[expires] => 1383730041
)
So this means deploy/public/index.php is properly linked to web2/public/index.php, right?
Well, even with the correct paths in the realpath_cache list, the respone still is ONE.
After rm deploy and ln -s web2 deploy Nginx was restarted, no effect.
Restarting php5-fpm after this gives the expected response of 'TWO'.
It's good to know that beside the index.php files, I did some test with .css and .js files.
After removing and recreating the symlink from/to web1 and web2, nginx will respond with the correct contents of the files.
What did I miss, what am I not seeing?
Configure your nginx using $realpath_root. It should help.
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
Kudos go to Vitaly Chirkov (https://stackoverflow.com/a/23904770/219272).
Once I altered the realpath_cache_ttl to '2' (That should fix it) the incorrect content was still showing.
After some digging in the loaded mods for php-fpm, I discovered that opcache was up and running. Disabling that will clear the cached realpath's when the ttl is over.
I don't wanna lower the realpath cache ttl to much,so I will settle in with a reload of php-fpm, since it is graceful.
I hope this thread and my answers will help others ;)
I had exactly same problem and none of the tricks did help. Beside all tricks listed on this page I ensured opcache is disabled then nginx cache was also disabled. I set
sendfile off;
It was described somewhere on stackoverflow.
I started to strace the php and nginx and it turned out that some libraries are read new location but also some were read from OLD location that symlink does not point to anymore.
On top of that updating the php script inside OLD directory was always showing properly - so that did not look like cache to me.
To make it more confusing command line worked fine and followed the symlink to NEW location.
I was pulling hair from my head over this!
It turned out that responsible for all this was the composer cache - it was caching some libraries.
I know not everyone uses it, but if you do and have similar problem it is worth checking.
I have vendor at the same level as the deployment scripts and I assume composer cache was causing a lot of confusion which location should be used.
After cleaning it up with
composer clear-cache
System started to behave as expected.
I hope it will help someone.
So I've got two apps I want to run on a server. One app I would like to be the "default" app--that is, all URLs should be sent this app by default, except for a certain path, lets call it /foo:
http://mydomain.com/ -> app1
http://mydomain.com/apples -> app1
http://mydomain.com/foo -> app2
My two rack apps are installed like so:
/var
/www
/apps
/app1
app.rb
config.ru
/public
/app2
app.rb
config.ru
/public
app1 -> apps/app1/public
app2 -> apps/app2/public
(app1 and app2 are symlinks to their respective apps' public directories). This is the Passenger setup for sub URIs described here: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rack_to_sub_uri
With the following config I've got /foo going to app2:
server {
listen 80;
server_name mydomain.com;
root /var/www;
passenger_enabled on;
passenger_base_uri /app1;
passenger_base_uri /app2;
location /foo {
rewrite ^.*$ /app2 last;
}
}
Now, how do I get app1 to pick up everything else? I've tried the following (placed after the location /foo directive), but I get a 500 with an infinite internal redirect:
location / {
rewrite ^(.*)$ /app1$1 last;
}
I hoped that the last directive would prevent that infinite redirect, but I guess not. My /foo rewrite still works. And I can still go to http://mydomain.com/app1.
Any ideas? Thanks!
One way is to create a new config.ru file that dispatches the request to the correct app.
# /var/www/apps/config.ru
require './app1/app1'
require './app2/app2'
map ('/') { run App1 }
map ('/foo') { run App2 }
Of course, this means your apps have to be made in a way that they can live on the same execution space.