Nginx Configuration Static Web server

Source: Internet
Author: User

To configure a static web, you need to implement a virtual host.

Step1: Preparing for work

1 View your network card address (my 192.168.223.135)

#ifconfig

We set up two Web servers here, so add a network card address.

#ifconfig eth0:0 192.168.223.145

2 Creating two site directories

#mkdir/var/tmp/website1

#mkdir/var/tmp/website2

3 Create two log directories

#mkdir/var/tmp/log/website1

#mkdir/var/tmp/log/website2

4 Creating two Test pages

#echo "This is WebSite1" >/var/tmp/website1/index.html

#echo "This is Website2" >/var/tmp/website2/index.html

Step2: Modify the configuration file and add a server node

1 Locate your Nginx installation directory, modify the configuration file

#vim/usr/local/nginx/conf/nginx.conf

Modify the server node in the configuration file

server {        listen 192.168.223.135:80;        server_name localhost;        #charset Koi8-r;        Access_log/var/tmp/log/website1/access.log;        Error_log/var/tmp/log/website1/error.log;        Location/{            root/var/tmp/website1;            Index index.html index.htm;        }        Error_page 404/404.html;        # REDIRECT Server error pages to the static page/50x.html        #        Error_page 502 503 504/50x.html;        Location =/50x.html {               root html;        }}

Add a server node

server {   listen 192.168.223.145:80;  server_name localhost;  #charset Koi8-r;  Access_log/var/tmp/log/website2/access.log;  Error_log/var/tmp/log/website2/error.log;  Location/{  root/var/tmp/website2;  Index index.html index.htm;  }  Error_page 404/404.html;  # REDIRECT Server error pages to the static page/50x.html  #  Error_page 502 503 504/50x.html;  Location =/50x.html {  root html;  }}
2 Start Nginx

#/usr/local/nginx/sbin/nginx

Client opens Web page, visits site http://192.168.223.135 http://192.168.223.145 try


If you feel that the input IP address is too ugly to access, it can also be modified to host header-based, for example: www.xxxxxxx.com form

Step 1: Modify server_name

We only need to modify the server_name in the two server nodes above.

The first modification is: server_name www.website1.com

The second modification is: server_name www.website2.com

Step2: Modifying the native Hosts file

In the Hosts file, add

192.168.223.135 www.website1.com

192.168.223.145 www.website2.com

#echo "192.168.223.135 www.website1.com" >>/etc/hosts

#echo "192.168.223.145 www.website2.com" >>/etc/hosts

Some machine hosts files may be read-only files, the modification requires root permission, you can do this son

To add a write permission to it:

#chmod a+w/etc/hosts

Then execute the above add instruction.

Step3: Restart Nginx

#pkill Nginx

#/usr/local/nginx/sbin/nginx

Then the client opens the browser and enters Www.website1.com www.website2.com to try.






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.