Use Nginx for reverse proxy (Configure virtual Host)

Source: Internet
Author: User
Tags web hosting

Requirements: Use a different domain name. Access the same IP, get to a different port server

(Linux server)

    1. Installation of Nginx required environment (may be installed)

      650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6F/A6/wKiom1Wj1b2Q17zcAACi8BA831s153.jpg "title=" 1.png " alt= "Wkiom1wj1b2q17zcaaci8ba831s153.jpg"/>

Nginx is a C language development, it is recommended to run on Linux , this tutorial uses Centos6.5 as the installation environment.

    • Gcc

installation nginx need to download the source code to compile, compile dependent gcc environment, if there is no gcc environment, need to install

Yum Install gcc-c++

    • PCRE

PCRE (perlcompatible Regular Expressions) is a Perl library that includes a Perl- compatible regular expression library. the nginx http module uses pcre to parse the regular expression, so the Pcre library needs to be installed on Linux .

Yuminstall-y Pcre Pcre-devel

Note:pcre-devel is a two-time development library developed using PCRE. Nginx also needs this library.

    • Zlib

Zlib Library provides a number of ways to compressand decompress, Nginx uses zlib to gzip the contents of the HTTP packet , so it needs to be installed on Linux zlib Library.

Yuminstall-y zlib Zlib-devel

    • Openssl

OpenSSL is a strong Secure Sockets Layer cipher library that includes key cryptographic algorithms, common key and certificate encapsulation management functions , and SSL protocols, and provides a rich set of applications for testing or other purposes.

Nginx not only supports the HTTP protocol, but also supports HTTPS(that is , it transmits HTTP on the SSL protocol ), so it needs to be installed in Linux OpenSSL Library.

Yuminstall-y OpenSSL Openssl-devel



2. Upload Nginx for decompression

tar-zxvf nginx-1.8.0.tar.gz


3. Go to unzip directory

CD nginx-1.8.0.tar.gz

4. Execute the following command to place the compiled post

The parameters are set as follows:

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client\

--http-proxy-temp-path=/var/temp/nginx/proxy\

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi\

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi\

--http-scgi-temp-path=/var/temp/nginx/scgi

Note: The temp file directory above is specified as /var/temp/nginx , need to be in/var under Create temp and nginx Catalogue

5. Create/var/temp/nginx

mkdir-p/var/temp/nginx


6. Compiling the installation

Make

Make install


7. View the directory after successful installation

cd/usr/local/nginx/

ll

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/A6/wKiom1Wj1c7h5HtBAADax9Yj7H0893.jpg "title=" 2DXK@0JEASWZAXGSK897PH7.png "alt=" Wkiom1wj1c7h5htbaadax9yj7h0893.jpg "/>

8. Start Nginx

cd/usr/local/nginx/sbin/

./nginx


9. View Nginx process, query whether to start

Ps-aux | grep nginx

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6F/A4/wKioL1Wj55XCIz6WAADSdvLp7c8482.jpg "title="%1lhl% CT (Kv_awt] (2vcl6k.png "alt=" Wkiol1wj55xciz6waadsdvlp7c8482.jpg "/>


10. Stop Nginx

mode 1, Quick stop:

Cd/usr/local/nginx/sbin

./nginx-s Stop

This method is equivalent to identifying the nginx process ID and then using the Kill command to force the kill process.

mode 2, complete stop ( recommended ):

Cd/usr/local/nginx/sbin

./nginx-s quit

This stop step is to wait until the Nginx process finishes processing the task to stop.


11. After modifying the configuration file, do not need to close, restart directly

when the nginx configuration file nginx.conf Modified, for the configuration to take effect need to restart nginx, use- s reload do not first stop Nginx re-start nginx can be configured in Nginx to take effect, as follows:

./nginx-s Reload


12. Test Installation Success

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6F/A4/wKioL1Wj6RqTtj5LAAGCrKaFpck138.jpg "title=" gry7p% E$znr]d}p3 ' Vy%tfj.png "alt=" Wkiol1wj6rqttj5laagcrkafpck138.jpg "/>




Demand:

Two domains point to the same server, and users visit different domain names to display different page content.

two domains are aaa.test.com and bbb.test.com

A server also uses virtual machine 192.168.101.3 instead


Configuration of the virtual host:

to modify the/usr/local/nginx/conf/nginx.conf file, add two virtual hosts as follows:

# Configure virtual host aaa.test.com

server {

# Listen for IP and ports, configure native IP and Ports

Listen 192.168.101.3:80;

# The virtual host name is aaa.test.com and the URL of the requesting domain name aaa.test.com will be resolved by this server configuration

server_name aaa.test.com;

# all requests begin with / start, all requests can match this location

Location/{

# Use the root command to specify the virtual host directory as the web hosting directory

# For example, visit http://ip/test.html will find /usr/local/aaa_html/test.html

# For example, visit http://ip/item/test.html will find /usr/local/aaa_html/item/test.html

root/usr/local/aaa_html;

# Specify Welcome page, search left-to-right order

Index index.html index.htm;

}

}

# Configure virtual host bbb.test.com

server {

Listen 192.168.101.3:80;

server_name bbb.test.com;

Location/{

root/usr/local/bbb_html;

Index index.html index.htm;

}

}






Use Nginx for reverse proxy (Configure virtual Host)

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.