Install and configure Nginx for reverse proxy in UbuntuVPS environment

Source: Internet
Author: User
Tags vps nginx reverse proxy
Debian and Ubuntu both come with Nginx and use them to configure the reverse proxy of Nginx, which is very convenient. Install Nginx and run the following command to install and run Nginxapt-getinstallnginx/etc/init. d/nginxstart and access port 80 of the IP address in the browser. the & quot; WelcometoNgin Debian and Ubuntu both come with Nginx and use them to configure the Nginx Reverse proxy, very convenient.

Install Nginx
Run the following command to install and run Nginx

Apt-get install nginx
/Etc/init. d/nginx start

Then access port 80 of the IP address in the browser and you will see "Welcome to Nginx! ", Which indicates Nginx installation is complete!



Configure Nginx for reverse proxy
The default site configuration file for Nginx is/etc/nginx/sites-available/default. modify the following parts of this file:
Location /{
Root/var/www/nginx-default;
Index index.html index.htm;
}

To:
Location /{
Proxy_pass http://www.6688.cc /;
Proxy_redirect off;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
}

Restart Nginx:

/Etc/init. d/nginx restart

Then re-access port 80 on the IP address in the browser. the google homepage is displayed, and the reverse proxy configuration is successful.

Multi-domain reverse proxy configuration instance
Configure a reverse proxy for multiple domain names on one VPS. for example, we have two domain names: test1.idcfree.com and test2.idcfree.com. we want the user to access test1.idcfree.com to view the content of www.linuxidc.com, if you want to see www.baidu.org.tw when you access test2.idcfree.com, you only know the existence of test1.idcfree.com and test2.idcfree.com, but do not know the existence of www.linuxidc.com and www.baidu.org.tw.

First, you need to direct the domain names test1.idcfree.com and test2.idcfree.com to the vps ip address.

Then add two files in the/etc/nginx/sites-available directory named test1.idcfree.com and test2.idcfree.com.

The content of the test1.idcfree.com file is as follows:
Server {
Listen 80;
Server_name test1.idcfree.com;

Location /{
Proxy_pass http://www.linuxidc.com /;
Proxy_redirect off;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
}
}

The content of the file test2.idcfree.com is as follows:
Server {
Listen 80;
Server_name test2.idcfree.com;

Location /{
Proxy_pass http://www.baidu.org.tw /;
Proxy_redirect off;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
}
}
Then run the following command:


Cd/etc/nginx/sites-enabled
Ln-sf/etc/nginx/sites-available/test1.idcfree.com. ln-sf/etc/nginx/sites-available/test2.idcfree.com.
/Etc/init. d/nginx restart

When you access test1.idcfree.com in your browser, the content of www.linuxidc.com will appear. when you access test2.idcfree.com, the content of www.baidu.org.tw will appear.

Advanced Configuration of reverse proxy
For some advanced configurations of Nginx Reverse proxy, we will continue to write blog introduction, so stay tuned.
 

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.