cphill
cphill

Reputation: 5914

Elasticbeanstalk Nginx Reverse Proxy Conf for Different Server

I am looking to reverse proxy a different server IP from my application when a user goes to /blog on my website. I have set up what I believe to be the correct nginx location block configuration and successfully deployed an ebextension with the configuration, but when I navigate to /blog I receive the 404 page that is rendered from my application's server. On further inspection the request shows that the request was made to the application IP and not the IP address found in the /blog location block in my nginx setup. Can anyone help determine what might be wrong with either my ebextension, nginx or application setup that is preventing the nginx location block to work? I'm not sure how to test beyond requesting the url and checking the network resource tab in my develop tools.

Here is my ebextensions folder setup:

> ebextensions
=> nginx
==> conf.d
==- 01_nginx_blog_rp.config
-00_nginx_https_rw.config
-02_sequelize_db_migration.config

01_nginx_blog_rp.config:

files:
  "/etc/nginx/conf.d/01_blog_proxy.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      client_max_body_size 10M;

  "/etc/nginx/conf.d/02_blog_location_block.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
        server {
            location /blog {
                proxy_pass http://xxx.xxx.xxx.xxx:2368;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
            }
        }

container_commands:
  01_reload_nginx:
    command: "sudo service nginx reload"

Upvotes: 1

Views: 788

Answers (1)

Igor K.
Igor K.

Reputation: 857

What you trying to achieve is not possible with just adding simple location block in conf.d.

You basically defining a new server block with location inside. This new server block is never used by nginx because you already have default server config generated by ElasticBeanstalk with configured listen and server_name directives. This two directives are used by nginx to decide where to forward request, therefore all incoming requests going to your application server and ignoring your customizations.

What you really need to do is to modify existing server block and add your location into it.

This can be done in a few different ways:

- Create new nginx config and delete default one.

In this aws documentation example they just adding a new .ebextensions/proxy.config and replace default nginx config with the custom one. Using this approach you can have a full control of your nginx configuration.

- Create new config and make it processed before default one.

If you still want to keep default config around you can just keep using your 00_nginx_https_rw.config name of config, this way nginx will process your config first, and if in your config you will have correct server_name and listen it will be used by nginx to process incoming request.

- Add hook to modify default config.

On AWS forum you can find another solution - add bash script hook to modify existing config.

Upvotes: 1

Related Questions