如何为nginx location指令配置.ebextensions?

tpxzln5u  于 2024-01-06  发布在  Nginx
关注(0)|答案(8)|浏览(162)

我想在Elastic Beanstalk中配置我的staging环境,以始终禁止所有spider。nginx指令如下所示:

  1. location /robots.txt {
  2. return 200 "User-agent: *\nDisallow: /";
  3. }

字符串
我知道我想在.ebextensions/文件夹下创建一个文件,例如01_nginx.config,但我不确定如何在其中构建YAML,以便它可以工作。我的目标是将此位置指令添加到现有配置中,而不必完全替换任何现有的配置文件。

lc8prwob

lc8prwob1#

有一种方法是在Amazon Linux 2上使用最新的.platform/nginx配置扩展(与旧的AMI相反)。
默认的nginx.conf在整个nginx.conf文件的两个位置包含配置部分。一个是直接在http块中,所以你不能在这里放置额外的location块,因为这在语法上是不法律的。第二个是在server块中,虽然,这是我们需要的。
第二个位置的部分文件包含在一个特殊的子目录.platform/nginx/conf.d/elasticbeanstalk中。将您的位置片段放在这里以添加位置块,如下所示:

  1. # .platform/nginx/conf.d/elasticbeanstalk/packs.conf
  2. location /packs {
  3. alias /var/app/current/public/packs;
  4. gzip_static on;
  5. gzip on;
  6. expires max;
  7. add_header Cache-Control public;
  8. }

字符串
您可以在反向代理配置下的文档中找到此内容。

sshcrbum

sshcrbum2#

我想做同样的事情。经过大量的挖掘,我发现了两种方法:

选项1.使用ebextension将nginx配置文件替换为自定义配置

我使用这个选项是因为它是最简单的一个。
按照Amazon在Using the AWS Elastic Beanstalk Node.js Platform - Configuring the Proxy Server - Example .ebextensions/proxy.config中给出的例子,我们可以看到他们创建了一个ebextension,创建了一个名为**/etc/nginx/conf.d/proxy.conf的文件,这个文件包含的内容和原来的nginx配置文件相同。然后,他们使用container_commands**删除了原来的nginx配置文件。
您需要将Amazon示例替换为当前nginx配置文件的内容。请注意,在containter命令中要删除的nginx配置文件也必须更新。我使用的配置文件有:

*nginx配置文件1:/opt/elasticbeanstalk/support/conf/webapp_healthd.conf
*nginx配置文件2:/etc/nginx/conf.d/webapp_healthd.conf

因此,对我有效的最终ebextension如下:

/.ebextensions/nginx_custom.config

  1. # Remove the default nginx configuration generated by elastic beanstalk and
  2. # add a custom configuration to include the custom location in the server block.
  3. # Note that the entire nginx configuration was taken from the generated /etc/nginx/conf.d/webapp_healthd.conf file
  4. # and then, we just added the extra location we needed.
  5. files:
  6. /etc/nginx/conf.d/proxy_custom.conf:
  7. mode: "000644"
  8. owner: root
  9. group: root
  10. content: |
  11. upstream my_app {
  12. server unix:///var/run/puma/my_app.sock;
  13. }
  14. log_format healthd '$msec"$uri"'
  15. '$status"$request_time"$upstream_response_time"'
  16. '$http_x_forwarded_for';
  17. server {
  18. listen 80;
  19. server_name _ localhost; # need to listen to localhost for worker tier
  20. if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
  21. set $year $1;
  22. set $month $2;
  23. set $day $3;
  24. set $hour $4;
  25. }
  26. access_log /var/log/nginx/access.log main;
  27. access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
  28. location / {
  29. proxy_pass http://my_app; # match the name of upstream directive which is defined above
  30. proxy_set_header Host $host;
  31. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  32. }
  33. location /assets {
  34. alias /var/app/current/public/assets;
  35. gzip_static on;
  36. gzip on;
  37. expires max;
  38. add_header Cache-Control public;
  39. }
  40. location /public {
  41. alias /var/app/current/public;
  42. gzip_static on;
  43. gzip on;
  44. expires max;
  45. add_header Cache-Control public;
  46. }
  47. location /robots.txt {
  48. return 200 "User-agent: *\nDisallow: /";
  49. }
  50. }
  51. container_commands:
  52. # Remove the default nginx configuration generated by elastic beanstalk
  53. removeconfig:
  54. command: "rm -f /opt/elasticbeanstalk/support/conf/webapp_healthd.conf /etc/nginx/conf.d/webapp_healthd.conf"

字符串
部署好这个改动之后,需要重新加载nginx服务器,可以使用eb ssh your-environment-name连接到自己的服务器,然后运行sudo service nginx reload

选项2.使用ebextension修改nginx配置文件生成器,使其在最终的nginx配置文件中包含您的自定义位置

第二个选项基于这篇文章:jabbermarky's answer in Amazon forums
他在他的回答中很好地解释了这个方法,所以我鼓励你阅读它,如果你想实现它。如果你要实现这个答案,你需要更新nginx文件配置生成器的位置。
请注意,我没有测试过这个选项。
总之,他添加了一个shell脚本,在生成nginx配置文件之前执行。在这个shell脚本中,他修改了nginx配置文件生成器,以在生成的nginx配置文件中包含他想要的服务器块位置。最后,他在最终nginx配置文件的服务器块中添加了一个包含他想要的位置的文件。

展开查看全部
doinxwow

doinxwow3#

看起来上面提到的方法已经不起作用了。新的方法是将nginx .conf文件放在.ebextensions的子文件夹中:
您现在可以将nginx.conf文件放在**.ebextensions/nginx文件夹中以覆盖Nginx配置。您也可以将配置文件放在.ebextensions/nginx/conf.d**文件夹中,以便将其包含在平台提供的Nginx配置中。
Source
这也不需要重新启动nginx,因为Elastic Beanstalk会处理这个问题。

yqkkidmi

yqkkidmi4#

嗯!.ebextensions
你可能最容易创建一个shell脚本来更改你的配置,然后运行它。真的不知道nginx,但尝试沿着行的东西:

  1. files:
  2. "/root/setup_nginx.sh" :
  3. mode: "000750"
  4. owner: root
  5. group: root
  6. content: |
  7. #!/bin/sh
  8. # Configure for NGINX
  9. grep robots.txt <your_config_file> > /dev/null 2>&1
  10. if [ $? -eq 1 ] ; then
  11. echo < EOF >> <your_config_file>
  12. location /robots.txt {
  13. return 200 "User-agent: *\nDisallow: /";
  14. }
  15. EOF
  16. # Restart any services you need restarting
  17. fi
  18. container_commands:
  19. 000-setup-nginx:
  20. command: /root/setup_nginx.sh

字符串
也就是说,首先创建一个schell脚本来完成你所需要的,然后运行它。
哦,小心你的YAML中没有制表符!只允许空格...检查日志文件/var/log/cfn_init.log是否有错误...
祝你好运!

展开查看全部
neekobn8

neekobn85#

对于版本Amazon Linux 2,在您的捆绑包中使用此路径,并将此文件夹压缩在一起
.platform/nginx/conf.d/elasticbeanstalk/000_my_custom.conf

bbmckpt7

bbmckpt76#

这就是我的工作:

  1. files:
  2. "/etc/nginx/conf.d/01_syncserver.conf":
  3. mode: "000755"
  4. owner: root
  5. group: root
  6. content: |
  7. # 10/7/17; See https://github.com/crspybits/SyncServerII/issues/35
  8. client_max_body_size 100M;
  9. # SyncServer uses some http request headers with underscores
  10. underscores_in_headers on;
  11. # 5/20/21; Trying to get the load balancer to respond with a 503
  12. server {
  13. listen 80;
  14. server_name _ localhost; # need to listen to localhost for worker tier
  15. location / {
  16. return 503;
  17. }
  18. }
  19. container_commands:
  20. 01_reload_nginx:
  21. command: pgrep nginx && service nginx reload || true

字符串

展开查看全部
chy5wohz

chy5wohz7#

要解决这个问题-你需要 Package 你的配置文件。如果你使用Docker,你应该有一个zip文件(我的文件名为deploy.zip),其中包含你的Dockerrun.aws.json。如果你没有-它很容易修改,只需通过zip -r deploy.zip Dockerrun.aws.json压缩你的部署
有了这个-你现在需要添加一个.platform文件夹如下:

  1. ├── .platform
  2.    └── nginx
  3.    └── conf.d
  4.    └── custom.conf

字符串
您可以随意命名您的custom.conf,并且可以拥有任意数量的文件。

  1. client_max_body_size 50M;


或者任何你想为你的配置。与此-修改您的zip现在是
zip -r deploy.zip Dockerrun.aws.json
然后部署。你的Nginx服务器现在将接受新命令

展开查看全部
jfgube3f

jfgube3f8#

这是可以实现的使用.ebextension配置文件,但我有困难踢nginx重新启动后,更改其配置文件。

  1. # .ebextensions/nginx.config
  2. files:
  3. "/etc/nginx/conf.d/robots.conf":
  4. mode: "000544"
  5. owner: root
  6. group: root
  7. content: |
  8. location /robots.txt {
  9. return 200 "User-agent: *\nDisallow: /";
  10. }
  11. encoding: plain

字符串
现在,我已经做了类似的添加一个文件来踢nginx轮胎,但由于一些奇怪的原因,它没有执行:

  1. "/opt/elasticbeanstalk/hooks/appdeploy/enact/03_restart_nginx.sh":
  2. mode: "000755"
  3. owner: root
  4. group: root
  5. content: |
  6. #!/usr/bin/env bash
  7. . /opt/elasticbeanstalk/containerfiles/envvars
  8. sudo service nginx restart
  9. ps aux | grep nginx > /home/ec2-user/nginx.times.log
  10. true
  11. encoding: plain

展开查看全部

相关问题