D
D
dexdev2015-06-15 21:50:38
Nginx
dexdev, 2015-06-15 21:50:38

How to properly configure nginx so that one domain belongs to one port?

I uploaded two sites to the VPS, configured unicorn on two different ports and nginx on two different ports, everything works except that when you click on a link like domain1.ru:81, it goes to the second site... how to bind the first domain to port 80 a the second to 81 and so that the second site does not open on the first domain when using the port of the second site?
nginx domain1

upstream unicorn {
    server unix:/home/demo/application/shared/run/unicorn.sock fail_timeout=0; 
}

server { 
    listen domain1.ru:80;
    server_name  domain1.ru;
    root /home/demo/application/current/public;
    try_files $uri/index.html $uri.html $uri @app;
    location ^~ /assets/ { 
        expires max;
        add_header Cache-Control public;
    }
    location @app {
        proxy_pass http://unicorn;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/demo/application/current/public;
    }
}

nginx domain2
upstream unicorn1 {
    server unix:/home/demo/application1/shared/run/unicorn.sock fail_timeout=0; 
}

server { 
    listen 81 default;
    server_name 148.176.100.46; 
    root /home/demo/application1/current/public;
    try_files $uri/index.html $uri.html $uri @app;
    location ^~ /assets/ { 
        expires max;
        add_header Cache-Control public;
    }
    location @app {
        proxy_pass http://unicorn1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/demo/application1/current/public;
    }
}

unicorn domain 1
worker_processes 4
root = '/home/demo/application'
rails_root  = "#{root}/current"
working_directory "/home/demo/application/current" # available in 0.94.0+
listen "#{root}/shared/run/unicorn.sock", :backlog => 64
listen 8080, :tcp_nopush => true
timeout 30
pid "#{root}/shared/run/unicorn.pid"
stderr_path "#{rails_root}/log/unicorn_error.log"
stdout_path "#{rails_root}/log/unicorn.log"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true
check_client_connection 
run_once = true

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
  if run_once
    run_once = false # prevent from firing again
  end
end

after_fork do |server, worker|

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection

end

unicorn second domain
worker_processes 4
root = '/home/demo/application1'
rails_root  = "#{root}/current"
working_directory "/home/demo/application1/current" # available in 0.94.0+
listen "#{root}/shared/run/unicorn.sock", :backlog => 64
listen 8081, :tcp_nopush => true
timeout 30
pid "#{root}/shared/run/unicorn.pid"
stderr_path "#{rails_root}/log/unicorn_error.log"
stdout_path "#{rails_root}/log/unicorn.log

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin, 2015-06-15
@AdilA

If you have a delimitation by ports, then you can make a "default" config for each port, which will process all connections that did not fit the "desired" server_name .
Although, to be honest, I don't quite understand why you need port 81? Hang up both sites on the 80th port + one config for "left" connections. For example, I use this:

server {
    listen   80 default; ## listen for ipv4
    listen   [::]:80 default ipv6only=on; ## listen for ipv6

    server_name  blahblahbla;
    access_log  /var/log/nginx/localhost.access.log;
    return      444;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question