Real-combat Nginx (1)-Virtual host base configuration

Source: Internet
Author: User
Tags nginx server

Nginx is a lightweight and high-performance WEB server, concurrent processing power, low resource consumption, whether it is a static server or small Web site, nginx performance is better, as a supplement to Apache and the use of more and more high-rate.

Add Nginx Virtual Host

This assumes that everyone's Nginx server has been installed. We can refer to the Apache configuration of the virtual host, directly in the main configuration file to refer to the virtual host configuration file, and the virtual host configuration file is stored in a specific virtual host directory;

1. We first create a directory of site resources;

[[email protected]/]# mkdir/www/vhosts/www{1,2}-pvmkdir:created directory '/www ' mkdir:created directory '/www/vhosts ' mkdir:created directory '/www/vhosts/www1 ' mkdir:created directory '/WWW/VHOSTS/WWW2 '

2. Create the virtual host configuration file directory;

[Email protected]/]# Mkdir/etc/nginx/extra

3. Start configuring the nginx.conf master configuration file;

[[email protected]/]# vim/etc/nginx/nginx.conf comment out all the contents of the predefined server{} segment in the http{} segment; add one below: Include extra/ nginx-vhost.conf;

650) this.width=650; "src=" "title=" 2.jpg " alt= "Wkiom1seqpxbuzadaabhlsxbp5a436.jpg"/>

4. Now we go directly to the virtual host configuration file directory to create a virtual host configuration file;

[[email protected] ~]# vim /etc/nginx/extra/ nginx-vhost.confserver {        listen        80;        server_name;         index index.html index.htm index.php;         root  /www/vhosts/www1;         log_format  ' $remote _addr -  $remote _user [$time _local]  $request '          ' $status   $body _bytes_sent  $http _referer   '          ' $http _user_agent  $http _x_forwarded_for ';         access_log  /var/log/nginx/;} 

Note: the logging here requires the debug log to be turned on when the installation of Nginx is compiled, the--with-debug parameter is added when compiling the installation, and if no parameters are added, an error is encountered while checking the syntax ;

5. Check the grammar;

[[Email protected] ~]# service Nginx configtest nginx: [Warn] the "log_format" directive May is used only on " HTTP "level in/etc/nginx/extra/nginx-vhost.conf:9nginx:the configuration file/etc/nginx/nginx.conf syntax is Oknginx: Configuration file/etc/nginx/nginx.conf test is successful

Let's just ignore the mistake here! I did not add the--with-debug parameter at compile time, but it does not affect the log record;

6. Put the test page;

[Email protected] ~]# echo "" >/www/vhosts/www1/index.html

7. Restart the Nginx service;

[[email protected] ~]# service nginx restartnginx: [warn] the  "Log_ Format " directive may be used only on " http " level in /etc/ nginx/extra/nginx-vhost.conf:9nginx: the configuration file /etc/nginx/nginx.conf  syntax is oknginx: configuration file /etc/nginx/nginx.conf test is  successfulstopping nginx:                                              [  OK  ] starting nginx: nginx: [warn] the  "Log_format"  directive may be  used only on  "http"  level in /etc/nginx/extra/nginx-vhost.conf:9                                                              [   OK  ]

8. Use ELinks to test website access;

[Email protected] ~]# elinks-dump[[email protected] ~]# elinks-dump HTTP://WW

9. View the log;

[[email protected] ~]# tail /var/log/nginx/ -  - [27/dec/2014:10:10:52 +0800] get / http/1.1404 570 - mozilla/ 5.0  (WINDOWS NT 6.1; WOW64)  AppleWebKit/537.36  (Khtml, like gecko)  chrome/37.0.2062.124 safari/537.36 - - - [27/dec/2014:10:10:52  +0800] GET /favicon.ico HTTP/1.1404 570 - Mozilla/5.0  (windows  NT 6.1; WOW64)  AppleWebKit/537.36  (Khtml, like gecko)  chrome/37.0.2062.124  safari/537.36 - - - [27/dec/2014:10:10:59 +0800] get / index.html http/1.1404 570 - mozilla/5.0  (WINDOWS NT 6.1; WOW64)   applewebkit/537.36  (Khtml, like gecko)  chrome/37.0.2062.124 safari/537.36 - - - [27/dec/2014:10:21:55 +0800] get / http/1.1404 168 - elinks/0.12pre5  ( textmode; linux; -)  - - - [27/dec/2014:10:22:04 +0800] get  / HTTP/1.1404 168 - ELinks/0.12pre5  (textmode; linux; -)  - - - [27/dec/2014:10:22:36 +0800] get / http/1.1404 168  - ELinks/0.12pre5  (textmode; linux; -)  - - - [27/dec /2014:10:23:04 +0800] get / http/1.1200 27 - elinks/0.12pre5  (TextMode ;  linux; -)  - - - [27/dec/2014:10:23:14 +0800] get /  HTTP/1.1200 27 - ELinks/0.12pre5  (textmode; linux; -)  -  - - [27/Dec/2014:10:30:10 +0800] GET / HTTP/1.1200 27 -  elinks/0.12pre5  (TextMode; linux; -)  - 

This article is from "Dragon Guardian" blog, please make sure to keep this source

Real-combat Nginx (1)-Virtual host base configuration

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: 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.