Nginx is used as the basic application of web servers

Source: Internet
Author: User
Nginx has unmatched performance in apache for static web pages, so the following describes the functions of nginx from the shortest to the deep: 1. Compile and install nginx1. initialize environment variables: # yumgroupinstall & quot; DevelopmentLibraries & quot; # yuminstallgccopens...

As nginx has unmatched performance in apache when doing static web pages, The following describes the role of nginx from the shortest to the deep:
1. Compile and install nginx
1. initialize environment variables:
# Yum groupinstall "Development Libraries"
# Yum install gcc openssl-devel pcre-devel zlib-devel
2. Compile and install nginx
# Tar xf nginx-1.1.3.tar.gz
# Cd nginx-1.1.3
./Configure \
-- Prefix =/usr \
-- Sbin-path =/usr/sbin/nginx \
-- Conf-path =/etc/nginx. conf \
-- Error-log-path =/var/log/nginx/error. log \
-- Http-log-path =/var/log/nginx/access. log \
-- Pid-path =/var/run/nginx. pid \
-- Lock-path =/var/lock/nginx. lock \
-- User = nginx \
-- Group = nginx \
With-http_ssl_module \
With-http_flv_module \
With-http_stub_status_module \
With-http_gzip_static_module \
-- Http-client-body-temp-path =/var/tmp/nginx/client /\
-- Http-proxy-temp-path =/var/tmp/nginx/proxy /\
-- Http-fastcgi-temp-path =/var/tmp/nginx/fcgi /\
-- With-pcre
# Make & make install
3. Compile the nginx startup script:
# Vim/etc/init. d/nginx
#! /Bin/sh
#
# Nginx-this script starts and stops the nginx daemon
#
# Chkconfig:-85 15
# Description: Nginx is an HTTP (S) server, HTTP (S) reverse \
# Proxy and IMAP/POP3 proxy server
# Processname: nginx
# Config:/etc/nginx. conf
# Config:/etc/sysconfig/nginx
# Pidfile:/var/run/nginx. pid
 
# Source function library.
./Etc/rc. d/init. d/functions
 
# Source networking configuration.
./Etc/sysconfig/network
 
# Check that networking is up.
["$ NETWORKING" = "no"] & exit 0
 
Nginx = "/usr/sbin/nginx"
Prog = $ (basename $ nginx)
 
NGINX_CONF_FILE = "/etc/nginx. conf"
 
[-F/etc/sysconfig/nginx] &./etc/sysconfig/nginx
 
Lockfile =/var/lock/subsys/nginx
 
Make_dirs (){
# Make required directories
User = 'nginx-V 2> & 1 | grep "configure arguments:" | sed's/[^ *] * -- user = \ ([^] * \). */\ 1/g '-'
Options = '$ nginx-V 2> & 1 | grep' configure arguments :''
For opt in $ options; do
If ['echo $ opt | grep'. *-temp-Path'']; then
Value = 'echo $ opt | cut-d "="-f 2'
If [! -D "$ value"]; then
# Echo "creating" $ value
Mkdir-p $ value & chown-R $ user $ value
Fi
Fi
Done
}
 
Start (){
[-X $ nginx] | exit 5
[-F $ NGINX_CONF_FILE] | exit 6
Make_dirs
Echo-n $ "Starting $ prog :"
Daemon $ nginx-c $ NGINX_CONF_FILE
Retval =$?
Echo
[$ Retval-eq 0] & touch $ lockfile
Return $ retval
}
 
Stop (){
Echo-n $ "Stopping $ prog :"
Killproc $ prog-QUIT
Retval =$?
Echo
[$ Retval-eq 0] & rm-f $ lockfile
Return $ retval
}
 
Restart (){
Configtest | return $?
Stop
Sleep 1
Start
}
 
Reload (){
Configtest | return $?
Echo-n $ "Reloading $ prog :"
Killproc $ nginx-HUP
RETVAL =$?
Echo
}
 
Force_reload (){
Restart
}
 
Configtest (){
$ Nginx-t-c $ NGINX_CONF_FILE
}
 
Rh_status (){
Status $ prog
}
 
Rh_status_q (){
Rh_status>/dev/null 2> & 1
}
 
Case "$1" in
Start)
Rh_status_q & exit 0
$1
;;
Stop)
Rh_status_q | exit 0
$1
;;
Restart | configtest)
$1
;;
Reload)
Rh_status_q | exit 7
$1
;;
Force-reload)
Force_reload
;;
Status)
Rh_status
;;
Condrestart | try-restart)
Rh_status_q | exit 0
;;
*)
Echo $ "Usage: $0 {start | stop | status | restart | condrestart | try-restart | reload | force-reload | configtest }"
Exit 2
Esac
# Chmod + x! $
4. start the nginx service
# Service nginx start
Enter http: // 192.168.0.78 for the test, as shown in:

2. since we have mentioned that we can provide web services, we will introduce how to use nginx to access virtual hosts.
1. port-based VM access
Based on the above installation process, we know that the web page is stored in/usr/HTML, so we can use different pages index.html and test.html for testing.
The content is nginx virtual one and nginx virtual two.
Now the preparations have been completed, and the next step is to configure the/etc/nginx. conf file.
The other settings remain unchanged by default. add two servers to http {...} and specify two listening ports as follows:
Server {
Listen 8080; // different ports
Server_name localhost;
Location /{
Root html;
Index test.html;
}
}
Server {
Listen 80; // different ports
Server_name localhost;
Location /{
Root html;
Index index.html;
}
}
# Service nginx reload // reload the configuration file
Enter http: // 192.168.0.78 and http: // 192.168.0.78: 8080 in the browser, as shown in:

 

2. Virtual IP-based host access
# Vim/etc/nginx. conf
Server {
Listen 80;
Server_name 192.168.0.78; // different IP addresses
Location /{
Root html;
Index test.html;
}
}
Server {
Listen 80;
Server_name 192.168.0.83; // different IP addresses
Location /{
Root html;
Index index.html;
}
}
My Nic configuration
# Ifconfig eth0 192.168.0.78
# Ifconfig eth0: 0 192.168.0.83
Enter http: // 192.168.0.78 and http: // 192.168.0.83 in the browser, as shown in.

 

3. host name-based VM
# Vim/etc/nginx. conf
Server {
Listen 80;
Server_name node1.luowei.com; // the host name is different.
Location /{
Root html;
Index test.html;
}
}
Server {
Listen 80;
Server_name node2.luowei.com; // the host name is different.
Location /{
Root html;
Index index.html;
}
}
Enter the http://node1.luowei.com and http://node2.luowei.com in the browser to test, as shown in

 

The experiment is complete and there is a certificate to be continued...
Author: "IT dream-Qi-sharing"

Related Article

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.