Nginx Virtual Host Configuration

Source: Internet
Author: User

Nginx Virtual Host Configuration

In Nginx, there are multiple ways to configure a VM: You can directly configure the VM in the main configuration file (Nginx. added in conf). You can also write a file for all virtual hosts and include it in the main configuration segment (include file name ), the last method is that each virtual host has a separate configuration file. This article uses the third method for demonstration.

First, make sure that Nginx has been installed. Next, create the virtual host configuration file and the directory where the virtual host file is located (my installation path is in/usr/local/Nginx)

Cd/usr/local/nginx/conf # Switch to the configuration file directory
Mkdir vhosts # directory where the VM configuration file is created
Touch vhosts/vhost1.conf # create the configuration file of the first VM

Add the following lines to the file:

Server {

Listen 80; # Set the listening port;

Server_name www.test.com; # Set the host name;

Location/{# define the website and directory;

Root/web/vhost/test1; # create a directory if the directory does not exist.

Index index.html; # This file also needs to be created in advance

}

Error_page 500 502 503 504/usr/local/nginx/html/50x.html; # error pages that define Page Status Codes as 5XX Series

Access_log/web/vhost/test1/logs/test1.access. log; # Set the access log Path

Error_log/web/vhost/test1/logs/test1.error. log crit; # Set the Error log Path

}

If the Directory and file on the top do not exist, follow the procedure below to create

Mkdir/web/vhost/test/logs-p

Touch/web/vhost/test/index.html

Edit the index.html file and add the content as needed. Then save and exit.

In this case, the configuration file of the VM has been fixed, but restarting the nginx service does not take effect because it is not included. Open nginx. the conf file must be added to the last line of the http {} segment.

Because it is used for testing, you may not be able to access the browser. In this case, you can modify the hosts file of the Local Machine and add the following content:

IP (the IP address of the web server) hostname (that is, the server_name we define)

 

After all the settings are complete, you can restart the nginx server, if no service startup script is added, use nginx-s quit & nginx to start it. (If the nginx sbin directory is not added to the environment variable, use the absolute path to start it/usr. /local/nginx/sbin/nginx-s quit &/usr/local/nginx/sbin/nginx)

View access results

Next, enable the status monitoring function for the VM. Add the following content to the configuration file of the VM.

Location/status {

Stub_status on; # Start status Function

Access_log off; # disable access logs.

}

Use nginx-t to check whether the configuration file is normal. If the configuration file is normal, restart the nginx service.

Enter www.test.com/status in the browser to check whether the status page is displayed.

There are 2 active connections, and we accept 5 requests to process 5 requests and respond 64 (I have been refreshing ..) Read 0 write 1 wait 1

Generally, these status pages cannot be accessed by unauthorized users. Therefore, we need to set user authentication and add the following content to the status page of the VM:

Auth_basic "admin ";

Auth_basic_user_file/web/vhost/test1/. passwd;

 

The following two methods are used to generate the. passwd file in the/web/vhost/test1/directory:

A. Generate directly using htpasswd

Htpasswd-c-m/web/vhost/test1/. passwd # If htpasswd is an httpd tool, install yun imstall httpd-tools.

B. manually create and add files after openssl Encryption

Openssl passwd-apr1-salt 'echo $ RANDOM'

Touch/web/vhost/test1/. passwd

The format of this file is:

User name: Password

Use nginx-t to check whether the configuration file is correct. If the configuration file is correct, repeat the nginx-s reload configuration file and open the browser to try to access it.

-------------------------------------- Split line --------------------------------------

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

-------------------------------------- Split line --------------------------------------

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

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.