Nginx reverse proxy to Apache

Source: Internet
Author: User
Tags nginx reverse proxy

Concept: Apache is responsible for PHP pages, nginx is responsible for static pages such as video files

For a single server, there is only one IP address, so different port numbers are used. Apache listens to port 80 and basically does not need to be changed. You only need to copy the video file to the file under nginx, then, the reverse proxy function is enabled. This module can send proxy requests to other servers. that is, reverse proxy of the video file to Apache:


1: Enable 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;

(2) Listening to port 8888

Can refer to a very good similar article: http://liuyu.blog.51cto.com/183345/166381

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

Problems that may occur when nginx is reversed to Apache:

(1) first, the real IP address will always be nginx:

Apache:

# Vim httpd. conf

Loadmodule rpaf_module modules/mod_rpaf-2.0.so

# Vim httpd-vhosts.conf.

Add

Rpafenable on

Rpafproxy_ips 192.168.1.100

Rpafsethostname on

Rpafheader X-forwarded-

Nginx has already been defined in proxy. conf

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

(2) session loss

Simple nginx round training will lead to session loss. In the face of this problem, a nginx scheduling algorithm can be changed to ip_hash, so that the load function will be meaningless, another method is to store the session in memcache .....

# Vim/usr/local/PHP/lib/PHP. ini

Change

Session. save_handler = memcache

Session. save_path = "TCP: // ip: 11211"

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

Hosts: liunx and win are available below. The main function is to direct the domain name to the specified IP address, and the domain name appears in the program will also point to the specified IP address.

Win: c: \ windows \ system32 \ drivers \ etc \ hosts

Liunx: ETC/hosts

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

As long as the nginx and Apache ports do not conflict with each other, the two servers can coexist.

[Supplement the above deficiencies]

A typical nginx + Apache application scheme can be that nginx occupies port 80, filters static requests, and then dynamically requests the proxy to the Apache port 8080. The advantage of proxy reverse proxy is that port 80 is always used during access, and visitors will not notice any difference.

However, some applications are really "smart". When we identify that the port on which Apache is located is 8080, we will add the related hyperlinks to the follow-up of 8080. So we are dead. Can we still have normal access ?! You can solve this problem by running Apache on port 80.
On the same server, there are nginx and Apache, and the two httpd services are both 80. Will they not conflict?

The following is an example.
Configuring nginx. conf
Server {
Listen 80;
SERVER_NAME www.abc.com;

Root/var/WWW

}
Modify it.
Server {
Listen public network ip123.123.123.123: 80; # specify that nginx only occupies port 80 of an IP address. # If your server has multiple IP addresses, you can specify multiple IP addresses.
SERVER_NAME www.abc.com;
}
If you have multiple virtual hosts in nginx, each of them must be modified in this way.

Bytes -----------------------------------------------------------------------------------------------------------------
Then it is Apache's turn to httpd. conf.
Set the original
Listen 80
Change
Listen 127.0.0.1: 80
The same as nginx, it specifies the IP address and port occupied by Apache.
Save and exit. Restart Apache to take effect.
If you have multiple virtual hosts on Apache. It does not need to be modified one by one like nginx, as long as all ports are 80.
For example:
Namevirtualhost *: 80
Serveradmin hello@abc.com
DocumentRoot/var/www/admin
Servername admin.abc.com
(Old Li: bind the background directory with the management background domain name, similar to manager. ***. com)

(In this case, I am afraid Apache can only access the service through http: // 127.0.0.1: 80, so it makes no sense if it still occupies port 80. It is better to use Apache 8080 and nginx 80.Without careful consideration, some "smart" applications have detected that Apache uses port 8080, and all the hyperlinks in the application are added to the end of port 8080. To solve this problem, that's why we have this article. In addition, most servers have two NICs. In addition to binding Apache to 127.0.0.1, they can also bind the IP address of another Nic. This problem is solved.)

In this way, nginx and Apache will only occupy port 80 of the specified IP address and will not conflict with each other.
Just adjust the nginx proxy parameters.
Smart Application problems 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.