Nginx Notes and Summary (a) Nginx implementation reverse proxy (Nginx + Apache dynamic separation)

Source: Internet
Author: User
Tags apache log nginx server nginx reverse proxy

In Nginx, Proxy is used to implement the reverse proxy, upstream is used to achieve load balancing.

For example, there are two servers, the Nginx server as a proxy server, execute. php files on the Apache server, the client sends the request to the Nginx server first, if the request is a. php file, the request is forwarded through the proxy pass to Apache server, Apache server after processing the results back to the Nginx server, Nginx service and return the results to the client. In this example, the Nginx server realizes the reverse proxy , or realizes the static and dynamic separation of Nginx + Apache.

Configuration process:

① first do not let the Nginx server execute the. php file, modify the/usr/local/nginx/conf/nginx.conf file, and comment The following location segment:

        Location ~ \.php$ {            #root           html;            #fastcgi_pass   127.0. 0.1:9000;            #fastcgi_index  index.php;            #fastcgi_param  script_filename  $document _root$fastcgi_script_name;            #include        fastcgi_params;        }

Save exit. Smooth restart Nginx.

Visit http://192.168.254.100/test.php at this time:

Nginx cannot parse the. php file.

② editing the Apache configuration file httpd.conf

[Email protected] nginx]# Find/-name httpd/usr/local/apache2/bin/httpd/root/httpd-2.2. /httpd/root/httpd-2.2. /.libs//usr/local/apache2/conf/httpd.conf

Modify the Listening port (Nginx has been listening to 80 ports, so the Apache listening port to 8080): Listen 8080

Save exit.

③ start Apache:

[[email protected] nginx]#/usr/local/apache2/bin/apachectl start

Visit http://192.168.254.100:8080:

④ Configure Apache's virtual host and port:

Modify Httpd-vhosts.conf:

Namevirtualhost *:8080 is for do and any < virtualhost> block.#<virtualhost *:8080>    "/usr/local/ nginx/html"    ServerName test.com</VirtualHost>

Define the DocumentRoot to the/usr/local/nginx/html directory. Save exit.

Edit httpd.conf:

Put

<directory/>    Options followsymlinks    allowoverride None    Order deny,allow    from  All </Directory>

Switch

<directory/>    Options followsymlinks    allowoverride all    Order deny,allow    from  All </Directory>

Put line:151

AllowOverride None

Switch

AllowOverride All

Save exit. Restart Apache.

⑤ Test: Access http://192.168.254.100:8080

Visit http://192.168.254.100:8080/ecshop/

⑥ Configuring an Nginx reverse proxy

[Email protected] nginx]# vim conf/nginx.conf

nginx.conf, modify location ~ \.php$, only need to add a sentence: Proxy_pass 192.168.254.100:8080;

        Location ~ \.php$ {            proxy_pass http:/192.168.  254.100:8080;            #root           html;            #fastcgi_pass   127.0. 0.1:9000;            #fastcgi_index  index.php;            #fastcgi_param  script_filename  $document _root$fastcgi_script_name;            #include        fastcgi_params;        }

Save exit. Smooth restart Nginx.

Test static and dynamic separation: Access http://192.168.254.100/test.php at this time

That is, the URL at this time without port 8080, access to the. php file, the same can be resolved, and through Apache parsing.

Test static and dynamic separation 2:

Modify/usr/local/nginx/html/test.php

test.php:

<? PHP // phpinfo (); echo rand (1,100000);? >'image/nginx.png'>

Save exit.

Visit http://192.168.254.100/test.php again

The Apache log is analyzed at this time:

Access.log:

Apache does not respond to pictures.

Re-analysis Nginx log:

The image of the nginx response.

Nginx Notes and Summary (a) Nginx implementation reverse proxy (Nginx + Apache dynamic separation)

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.