Help configure Apache. I have several sites included in apache. There is a ports.conf file in which Listen: 80 is written, which almost suits me. But for one single domain I want to change this very Listen from port 80 to port 8080. What I want to achieve: I want the handler to connect Nginx but not for all sites on the server, but only for one. In the apache file of this site I have

<VirtualHost *:8080> <Directory /var/www/html/my-domain> Options FollowSymLinks AllowOverride All </Directory> ServerAdmin admin@mail.ru ServerName my-domain.ru DocumentRoot /var/www/html/my-domain ErrorLog ${APACHE_LOG_DIR}/error-my-domain.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

I have written in the Nginx conf file of this site

 server { listen 80; root /var/www/html/my-domain; index index.php index.html index.htm; server_name my-domain.ru; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080; } location ~ /\.ht { deny all; } } 

Both Apache and Nginx are running, but when entering my-domain.ru, Apache is included in the processing. And when I go to my-domain.ru:8080 Nginx works.

Contents of the /etc/nginx/nginx.conf file

 user www-data; worker_processes 2; # Ставим число по количеству ядер timer_resolution 100ms; worker_rlimit_nofile 8192; worker_priority -5; #Увеличитвваем приоритет error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { access_log /var/log/nginx/access.log; sendfile on; keepalive_timeout 65; tcp_nodelay on; gzip on; gzip_min_length 1100; #gzip_disable "msie6"; #Быстрее, но работает только на новых версиях nginx gzip_disable "MSIE [1-6]\.(?!.*SV1)"; gzip_proxied any; gzip_comp_level 4; gzip_types text/plain text/css application/x-javascript text/xml application/xml $ gzip_vary on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 
  • two processes cannot simultaneously listen to the same tcp port, more precisely, the same ip: port combination. but in the case of http servers, you can proxy connections from one http server to another (listening to another port, of course). - aleksandr barakin
  • Thanks for the tip! It was possible to get what I wanted by adding the necessary site to conf Apache for such directives as ProxyPass / http://localhost:8080 ProxyPreserveHost On <Proxy *> Order deny,allow Allow from all </Proxy>
  • So write a complete answer that could help not only you, but also others. - aleksandr barakin

1 answer 1

You can redirect a request for processing via nGinx from Apache by adding directives to the file of the site you need to conf

 ProxyPass / http://localhost:8080 ProxyPreserveHost On <Proxy *> Order deny,allow Allow from all </Proxy> 

8080 - in this case, the port that listens to Nginx Also some information on the ProxyPreserveHost directive

Pay attention to the ProxyPreserveHost option; when proxying, the server replaces the host parameter in the request header to the host from the ProxyPass option, while copying the old host to the X-Forwarded-Server parameter. In this case, on some engines (sites), redirect may not work properly substituting the local address of the internal server when an external one is needed. To solve this problem, you need to enable the ProxyPreserveHost option, then the server will leave the real host that came to the current server.

Source http://www.py-my.ru/post/4bfb3c691d41c846bc00001f

  • Only after all, it is usually redirected to apache from nginx, and not vice versa :) - andreymal