Centos 7 (2) Configure nginx reverse proxy

Source: Internet
Author: User
Tags nginx reverse proxy

Centos 7 (2) Configure nginx reverse proxy

Nginx can run the PHP program through PHP-FPM, or switch to Apache, so that Apache can call the PHP program to run it.
However, for nginx, its reverse proxy function is more worth studying. Next we will configure nginx reverse proxy for three Apache servers, and configure memcache as the session storage path.
1. Environment
Centos 7, 192.168.1.14, Apache 80, nginx 808
Centos 7, 192.168.1.12, Apache 80
Ubuntu 14.04 server, 192.168.1.161, Apache 80
2. Configure nginx and modify nginx. conf as follows:
HTTP {
Include mime. types;
Default_type application/octet-stream;

# Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
# '$ Status $ body_bytes_sent "$ http_referer "'
# '"$ Http_user_agent" "$ http_x_forwarded_for "';

# Access_log logs/access. Log main;

Sendfile on;
# Tcp_nopush on;

# Keepalive_timeout 0;
Keepalive_timeout 65;

# Gzip on;


Upstream php_server_pool {
Server 192.168.1.12: 80 Weight = 4 max_fails = 2 fail_timeout = 30 s;
Server 192.168.1.14: 80 Weight = 4 max_fails = 2 fail_timeout = 30 s;
Server 192.168.1.161: 80 Weight = 4 max_fails = 2 fail_timeout = 30 s;
}
Server {
Listen 808;
SERVER_NAME localhost;

# Charset koi8-r;

# Access_log logs/host. Access. Log main;

Location /{
Root HTML;
Index index.html index.htm;

Proxy_next_upstream http_502 http_504 error timeout invalid_header;
Proxy_pass http: // php_server_pool;
Proxy_set_header host www.shiyq.com;
Proxy_set_header X-forwarded-for $ remote_addr;
}

Add the upstream tag at the next level of HTTP and name it php_server_pool. This is a server pool and three hosts are added, which are 12, 14, and 161 respectively. The weights are the same and the number of failures is two, the failure time is 30 seconds.
Add under location/at the lower level of server
Proxy_next_upstream http_502 http_504 error timeout invalid_header;
Proxy_pass http: // php_server_pool;
Proxy_set_header host www.shiyq.com;
Proxy_set_header X-forwarded-for $ remote_addr;
It is easy to understand that if the header 502, 504, times out, or is invalid, it will be transferred to the next machine, and the root directory will be transferred to the root directory of a machine in the server pool.
Restart nginx and kill-HUP nginx
In the root directory of the three machines, edit the test file and output the accessed host address.
Vim/etc/www/html/Hello. php
<? PHP
Echo $ _ server ['server _ ADDR ']. 'weclome you! ';
?>
Enter http: // 192.168.1.14: 808/Hello. php In the browser
According to the page output, we can see that different machines are actually switched.
3. Use memcache to save the session
It is very easy to use nginx for reverse proxy, but the machine will be switched every time you access the service (the weights are the same). You can use the ip_hash command to prevent nginx from switching the machine for the same session, for the time being, it is not easy to test the environment. You do not know whether the switchover is invalid or whether ip_hash takes effect. I will study this issue later, here, we use memcache to save the session to solve the switching problem.
1) install memcache
Centos 7: Yum install memcached PHP-PECL-memcache
Ubuntu 14.04: sudo apt-Get install php5-memcache memcached
2) Start memcache
Start memcache on 192.168.1.12
Memcached-u Root
Memcached has many parameters. You can specify the memory, port, and so on. If you are free, you can study it. The default port is 12111. If you are a root user, you must specify the-u parameter.
Configure the firewall and open ports
For iptables, VIM/etc/sysconfig/iptables, port 22 is opened in the file by default, as shown below:
-A input-p tcp-M state -- state new-m tcp -- dport 22-J accept
Copy this line, change 22 to 11211, and restart the firewall.
For firewalld, run
Firewall-cmd -- add-Port = 11211/tcp
Firewall-cmd -- Permanent -- add-Port = 11211/tcp
Test Results
Telent 192.168.1.12 12111
If you can log on, it indicates that the startup is normal.
3) Modify PHP. ini to support memcache session Storage
Ubuntu 14.04, sudo Vim/etc/PhP5/apache2/PHP. ini. Modify the file as follows:
Session. save_handler = memcache
Session. save_path = "192.168.1.12: 11211"
Centos 7: Modify two files:/etc/PHP. ini,/etc/httpd/CONF. d/PHP. conf
Session. save_handler = memcache
Session. save_path = "192.168.1.12: 11211"
Be sure to modify the php. conf file. Otherwise, it will be stored in the file by default.
In info. php, we can see the session. save_path and session. save_handler of the session. There are two columns: local value and master value, which must be changed. Remember.
4) Test Results
Edit files in the root directory
Vim login. php
<? PHP

Echo $ _ server ['server _ ADDR ']. 'Welcome you! ';
Session_start ();

$ _ Session ['name'] = 'shi Yongqiang-14 ';
$ _ Session ['pwd'] = 'password tt ';
$ Session_id = session_id ();
Echo "Sid:". $ session_id. "<br> ";
Echo '<br> <a href = "./read. php"> Read session </a> ';

?>

Vim read. php
<? PHP

Echo $ _ server ['server _ ADDR ']. 'Welcome you ';
Session_start ();

$ Session_id = session_id ();
Echo "Sid:". $ session_id. "<br> ";

Echo 'name: '. $ _ session ['name']. "<br> ";
Echo 'pwd: '. $ _ session ['pwd']. "<br> ";

Echo '<br> <a href = "./login. php"> login session </a> ';
?>
Note that on different machines, the end of the name variable is the same as the last number of Host IP addresses, such as Shi Yongqiang-161 and Shi Yongqiang-14.
Refresh http: // 192.168.1.14: 808/login. PHP, you can see the address is changing, Click Read session, you can see the last session information, refresh the page, although the host is switched, but the session information remains unchanged.
You can also see via Telnet
[[Email protected] HTML] # telnet 192.168.1.12 11211.
Trying 192.168.1.12...
Connected to 192.168.1.12.
Escape Character is '^]'.
Get pd8k8rju5g4fpbfmm5kuaid3j0
Value pd8k8rju5g4fpbfmm5kuaid3j0 0 42
Name | S: 12: "Shi Yongqiang-12"; PWD | S: 6: "password ";
End
Pd8k8rju5g4fpbfmm5kuaid3j0 is the session ID. You can see that memcache saves the session and can share it.

Related Article

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.