0

I'm trying to set up multi websites with different ports but same domain. i.e. domain.com, domain.com:81, domain.com:82 Below is the my nginx configuration,

/etc/nginx/sites-enabled/magento225

and

upstream fastcgi_backend {
        server  unix:/run/php/php7.1-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;
    server_name domain.com;
    return 301 https://$server_name$request_uri;
}


server {
    listen 81;
    listen [::]:81;
    server_name domain.com;
    return 301 https://$server_name$request_uri;
    root /var/www/html/magento226;
    location /  {
        proxy_redirect http://127.0.0.1:81/;
    }
}


server {

        listen 443 ssl;
        server_name domain.com;

        ssl on;
        ssl_certificate /etc/letsencrypt/live/wpdev1.tk/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/wpdev1.tk/privkey.pem;

        set $MAGE_ROOT /var/www/html/magento225;
        set $MAGE_MODE developer;
        include /var/www/html/magento225/nginx.conf.sample;
}

After that nginx is failed to restart. I tried with removing below piece of code

location  {
            proxy_redirect http://127.0.0.1:81/;
        }

This time nginx can be restarted but website is not working. i.e. domain.com:81

I have enabled the port (81) from the ec2 instance by updating security rule. Any ideas would be appreciated. Thanks.

Update 1:

upstream fastcgi_backend {
        server  unix:/run/php/php7.1-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;
    server_name domain.com;
    root /var/www/html/magento225;
}


server {
    listen 81;
    listen [::]:81;
    server_name domain.com;
    root /var/www/html/magento226;
    location /  {
        proxy_pass http://127.0.0.1:81/;
    }
}

Tried above configuration and not able to connect 81 port.

Elavarasan
  • 109
  • 3
  • You are missing a `/` or whatever before your location block. `location / { ... }`, or `location /81/ { proxy_redirect http://127.0.0.1:81/; }` – Lenniey Apr 01 '19 at 12:06
  • @Lenniey Thanks. That was typo. It is not working with location / also. – Elavarasan Apr 01 '19 at 12:15
  • That was only an observation, no solution. I suppose you are mixing up your ports, redirects, ssl, and so on. E.g. `$server_name` will not preserve port information of the request, and you don't have the `proxy_redirect` in your ssl config etc. You have to make up your mind what should be redirected where and with or without `ssl`. – Lenniey Apr 01 '19 at 12:41
  • `proxy_redirect` requires two parameters. It is useless without a `proxy_pass` statement. Did you mean `proxy_pass`? Are you trying to `proxy_pass` to itself (that would create an infinite loop). The `return` statement will execute before the `location` block is reached. – Richard Smith Apr 01 '19 at 12:46
  • Thanks, @Lenniey and Richard Smith. I have updated my question with my current nginx configuration. And I removed ssl thing, will configure it later. I just need to these 2 websites work on two different ports. I'm new this nginx configuration. – Elavarasan Apr 01 '19 at 13:20
  • What is the error that you received? – Michael Hampton Apr 01 '19 at 15:46
  • Your updated configuration shows that nginx is listening on port 81, and proxies all requests to port 81 back to itself. This cannot work. Please elaborate what are the different components of your hosting setup and where each component should serve its content. – Tero Kilkanen Apr 02 '19 at 19:29

1 Answers1

3

Seems like, in your update, the port 81 server block is direct traffic back to itself with that proxy_pass statement, which would be a reason for it not to return a request at all.

Are you trying to hit the upstream defined there? You'd want to change:

proxy_pass http://127.0.0.1:81/;

to

proxy_pass http://fastcgi_backend/;

if that's the case.

Joel Kleier
  • 131
  • 1
  • Instead of writing `fastcgi_backend`, it should be `upstream_server` or similar. `fastcgi` and `proxy_pass` are incompatible concepts. – Tero Kilkanen Apr 01 '19 at 19:36
  • 'fastcgi_backend' in the answer's example is used because it's the same name for the upstream in the question. – Joel Kleier Apr 01 '19 at 19:54
  • I see that now. However, using `proxy_pass` with a PHP-FPM Unix socket does not work, because they are different protocols. – Tero Kilkanen Apr 02 '19 at 19:30