C
C
csergey2014-04-17 18:03:27
Nginx
csergey, 2014-04-17 18:03:27

How to separate sites on the backend?

Nginx front Apache back.
Apache has two virtual sites site1 and site2
How to make nginx return site1 in one case and site2 in another (don't separate by ports)?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
O
Oleg Burca, 2014-04-18
@Cram

Firstly, nginx should listen on port 80 (only external ip is allowed), and apache on another one, for example 81 (only ip 127.0.0.1 is allowed).
Further, for everything to work well - both in nginx and in apache, the same sites must be configured separately - this way there will be no confusion, and it will be possible to make changes to the settings for each site separately.
Accordingly, the settings:
Move all reverse-proxy nginx settings to a separate file:
>>> /etc/nginx/proxy.conf >>>

# настройка проксирования
proxy_redirect off;
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_cache_key "$scheme://$host$request_uri $do_not_cache";
proxy_cache cache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;

>>> /etc/nginx/sites-available/site1.ru.conf >>>
server {
listen 123.123.123.123:80;
server_name www.site1.ru site1.ru;
access_log /var/log/nginx/site1.ru_nginx_access_log;
error_log  /var/log/nginx/site1.ru_nginx_error_log;
location / {
include /etc/nginx/proxy.conf;
proxy_pass http://site1.ru:81;
}
#Статику отдаём напрямик
location ~* ^.+\.(jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mp3)$ {
expires 30d;
root /home/site1.ru/public_html;}
# Запрещаем открытие .htaccess через браузер
location ~ /\.ht {deny all;}
}

>>> /etc/nginx/sites-available/site2.ru.conf >>>
server {
listen 123.123.123.123:80;
server_name www.site2.ru site2.ru;
access_log /var/log/nginx/site2.ru_nginx_access_log;
error_log  /var/log/nginx/site2.ru_nginx_error_log;
location / {
include /etc/nginx/proxy.conf;
proxy_pass http://site2.ru:81;
}
#Статику отдаём напрямик
location ~* ^.+\.(jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mp3)$ {
expires 30d;
root /home/site2.ru/public_html;}
# Запрещаем открытие .htaccess через браузер
location ~ /\.ht {deny all;}
}

>>> /etc/apache2/sites-available/site1.ru.conf >>>
<VirtualHost 127.0.0.1:81>
ServerName site1.ru
ServerAlias www.site1.ru
DocumentRoot /home/site1.ru/public_html
...
</VirtualHost>

>>> /etc/apache2/sites-available/site2.ru.conf >>>
<VirtualHost 127.0.0.1:81>
ServerName site2.ru
ServerAlias www.site2.ru
DocumentRoot /home/site2.ru/public_html
...
</VirtualHost>

C
csergey, 2014-04-17
@csergey

Sorry for the inaccurate question
server {
listen 80;
server_name site3
location /site1 {
proxy_pass http://site1:88;
proxy_redirect off;
proxy_intercept_errors on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
server {
listen 80;
server_name site4
location /site2 {
proxy_pass http://site2:88;
proxy_redirect off;
proxy_intercept_errors on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
In this version does not work

I
Ivan, 2014-04-17
@0neS

If nginx is the front and apache is the back, then what's the point of nginx distinguishing which site the request goes to? It should redirect all requests to apache, and that, in turn, should give the desired content.

C
csergey, 2014-04-18
@csergey

That no one knows how to set up such a multi-domain web server??

C
csergey, 2014-04-18
@csergey

Let me explain the question again
Nginx is the frontend, Apache is the backend. In the nginx config, you need to register two virtual hosts site3 and site4. What would site3 on the backend give out site1, and site4 give out site2.

C
csergey, 2014-04-18
@csergey

Thanks for the answer. in principle, I realized that it is better to make such sites with the same names

I
Ivan, 2014-04-20
@0neS

upstream backend {
  server 127.0.0.1:8080;
}

server {
  listen 80;
  server_name 12.23.34.45; /* внешний ip-адрес сервера */
  
  location / {
    proxy_pass http://backend/;
    proxy_redirect off;
    proxy_intercept_errors on;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
  location ~ /\.ht {
    deny all;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question