Nginx Reverse proxy to Apache

Source: Internet
Author: User
Tags nginx reverse proxy

Idea: Apache is responsible for PHP pages, Nginx is responsible for static pages such as video files

A single server, there is only one IP, that is the use of different port number, Apache listening to 80 ports, basically do not need to change what, just want to copy the video file to Nginx under the file, and then turn on the reverse proxy function, reverse proxy is: This module can be agent request to other servers. That is, the video file reverse proxy to Apache below:


1: Turn on the reverse proxy service

Reverse to Port 80

You can also add the following parameters:

Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header remote-host $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;


⑵ listens on port 8888 .


You can refer to a very good article like this: http://liuyu.blog.51cto.com/183345/166381

***********************************************************************

Nginx Reverse to Apache problems that may occur:

⑴ first access to real IP will always be nginx:

Apache side:

#vim httpd.conf

LoadModule Rpaf_module modules/mod_rpaf-2.0.so

#vim httpd-vhosts.conf

Add in the virtual host

Rpafenable on

Rpafproxy_ips 192.168.1.100

Rpafsethostname on

Rpafheader x-forwarded-for

The Nginx end has been defined in proxy.conf.

Detailed solution reference; http://windphp.com/unix/52.html

⑵session lost

Nginx simple rotation will lead to session loss, faced with this problem, one can be nginx scheduling algorithm for Ip_hash, so that the role of the load will have no meaning, the other can be a session in the memcache ....

#vim/usr/local/php/lib/php.ini

Amended to

Session.save_handler = Memcache

Session.save_path = "tcp://ip:11211" *************************************************

Hosts:liunx and win below all have, the main function is to redirect the domain name to the designated IP, and the place where the domain name appears in the program will point to the specified IP.

Win below: C:\WINDOWS\system32\drivers\etc\HOSTS

Liunx below: etc/hosts

======================================================

As long as the Nginx and Apache are guaranteed to not conflict two servers can coexist

"Supplement to the above deficiencies"

A typical Nginx + Apache application can be Nginx to occupy 80 ports, filter static requests, and then dynamically request Proxy to Apache's 8080 port. The advantage of proxy reverse proxy is that when you visit, it is always 80 ports, and visitors will not be aware of any difference.

But some applications are very "smart", the identification of Apache is located in the port is 8080, will be associated with hyperlinks are added: 8080 follow-up. So you're dead, and you can have regular access. One way to solve this problem is to run Apache on port 80 as well.
The same server, there are nginx also have apache,2 a httpd service, are 80, will not conflict.

The bottom is the example method.
In the configuration of nginx.conf
server {
Listen 80;
server_name www.ABC.com;

Root/var/www

}
Modify it.
server {
Listen public network ip123.123.123.123:80; #指定Nginx只占用某个IP的80端口. #如果你服务器中有多个IP, you can also specify multiple.
server_name www.ABC.com;
}
If you have multiple virtual hosts in Nginx, each one needs to be modified.

--------------------------------------------------------------------------------------------------------------- --
And then the httpd.conf of Apache.
Put the original
Listen 80
To
Listen 127.0.0.1:80
As with Nginx, specify the IP and port that Apache uses.
Save exit, restart Apache to take effect.
If you have more than one virtual host on Apache. There is no need to modify it as nginx, as long as it is 80 ports.
Such as:
Namevirtualhost *:80
ServerAdmin hello@abc.com
Documentroot/var/www/admin
ServerName Admin. ABC.com
( old Li annotation: here with admin background domain name binding background directory, similar to manager.***.com)

(So, I'm afraid Apache can only access it through http://127.0.0.1:80, so it doesn't make sense to take up 80 ports.) Not as good as Apache with 8080,nginx 80. not seriously, there is a "smart" application detects that Apache uses 8080 ports, will put all the hyperlinks in the application plus: 8080 end, in order to solve the problem, so this article. And most servers have 2 cards, in addition to the Apache binding in 127.0.0.1 can also bind another network card IP, so the problem is solved. )

In this way, Nginx and Apache will only occupy 80 ports of the specified IP and will not conflict.
Just adjust the parameters of the Nginx proxy.
The "smart" application problem can be solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.