12.6 Nginx Installation
Cd/usr/local/src
wget http://nginx.org/download/nginx-1.8.0.tar.gz
Tar zxvf nginx-1.8.0.tar.gz
./configure--prefix=/usr/local/nginx
Make && make install
See if the configuration file is wrong-t
/usr/local/nginx/sbin/nginx-t
To start script editing:
Vim/etc/init.d/nginx//Copy the following (refer to Https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx)
Content is
--------------------------------------------------------------
#!/bin/bash
# Chkconfig:-30 21
# Description:http Service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
Nginx_sbin= "/usr/local/nginx/sbin/nginx"
nginx_conf= "/usr/local/nginx/conf/nginx.conf"
Nginx_pid= "/usr/local/nginx/logs/nginx.pid"
Retval=0
Prog= "Nginx"
Start ()
{
Echo-n $ "Starting $prog:"
Mkdir-p/dev/shm/nginx_temp
Daemon $NGINX _sbin-c $NGINX _conf
Retval=$?
Echo
Return $RETVAL
}
Stop ()
{
Echo-n $ "Stopping $prog:"
Killproc-p $NGINX _pid $NGINX _sbin-term
Rm-rf/dev/shm/nginx_temp
Retval=$?
Echo
Return $RETVAL
}
Reload ()
{
Echo-n $ "Reloading $prog:"
Killproc-p $NGINX _pid $NGINX _sbin-hup
Retval=$?
Echo
Return $RETVAL
}
Restart ()
{
Stop
Start
}
Configtest ()
{
$NGINX _sbin-c $NGINX _conf-t
return 0
}
Case "$" in
Start
Start
;;
Stop
Stop
;;
Reload
Reload
;;
Restart
Restart
;;
Configtest)
Configtest
;;
*)
echo $ "Usage: $ {start|stop|reload|restart|configtest}"
Retval=1
Esac
Exit $RETVAL
--------------------------------------------------------------
chmod 755/etc/init.d/nginx
Chkconfig--add Nginx
Chkconfig Nginx on
CD/USR/LOCAL/NGINX/CONF/;MV nginx.conf Nginx.conf.bak
Vim nginx.conf//write the following (refer to https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)
Content is
--------------------------------------------------------------
User nobody nobody;
Worker_processes 2;
Error_log/usr/local/nginx/logs/nginx_error.log Crit;
Pid/usr/local/nginx/logs/nginx.pid;
Worker_rlimit_nofile 51200;
Events
{
Use Epoll;
Worker_connections 6000;
}
http
{
Include Mime.types;
Default_type Application/octet-stream;
Server_names_hash_bucket_size 3526;
Server_names_hash_max_size 4096;
Log_format combined_realip ' $remote _addr $http _x_forwarded_for [$time _local] '
' $host ' $request _uri "$status"
' "$http _referer" "$http _user_agent";
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 30;
Client_header_timeout 3m;
Client_body_timeout 3m;
Send_timeout 3m;
Connection_pool_size 256;
Client_header_buffer_size 1k;
Large_client_header_buffers 8 4k;
Request_pool_size 4k;
Output_buffers 4 32k;
Postpone_output 1460;
Client_max_body_size 10m;
Client_body_buffer_size 256k;
Client_body_temp_path/usr/local/nginx/client_body_temp;
Proxy_temp_path/usr/local/nginx/proxy_temp;
Fastcgi_temp_path/usr/local/nginx/fastcgi_temp;
Fastcgi_intercept_errors on;
Tcp_nodelay on;
gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 8k;
Gzip_comp_level 5;
Gzip_http_version 1.1;
Gzip_types text/plain application/x-javascript text/css text/htm
Application/xml;
Server
{
Listen 80;
server_name localhost;
Index index.html index.htm index.php;
root/usr/local/nginx/html;
Location ~ \.php$
{
Include Fastcgi_params;
Fastcgi_pass Unix:/tmp/php-fcgi.sock;
Fastcgi_index index.php;
Fastcgi_param Script_filename/usr/local/nginx/html$fastcgi_script_name;
}
}
}
--------------------------------------------------------------
/usr/local/nginx/sbin/nginx-t
/etc/init.d/nginx start
PS aux | grep nginx
NETSTAT-LNTP | grep 80
Test #curl localhost
Test PHP
vim/usr/local/nginx/html/1.php
Write the following content
----------------------------------------------------------------
<?php
echo "This is Nginx test page";
----------------------------------------------------------------
Test Curl localhost/1.php
12.7 Default Virtual Host
Add the following line in the vim/usr/local/nginx/conf/nginx.conf//http
Include vhost/*.conf;
Mkdir/usr/local/nginx/conf/vhost
CD!$;vim default.conf//Add the following:
----------------------------------------------------------------
Server
{
Listen default_server; With this tag is the default virtual host
server_name aaa.com;
Index index.html index.htm index.php;
Root/data/wwwroot/default;
}
----------------------------------------------------------------
Mkdir-p/data/wwwroot/default
CD /data/wwwroot/default
echo "This is a default site." >/data/wwwroot/default/index.html
/USR/LOCAL/NGINX/SBIN/NGINX-T//Check configuration for errors
/usr/local/nginx/sbin/nginx-s Reload//reload, can also restart service
or/etc/init.d/nginx restart//Restart service
Curl localhost
Curl-x127.0.0.1:80 123.com
12.8 Nginx user authentication
vim/usr/local/nginx/conf/vhost/test.com.conf//write the following:
----------------------------------------------------------------
Server
{
Listen 80;
server_name test.com;
Index index.html index.htm index.php;
root/data/wwwroot/test.com;
Location/
{auth_basic "auth";
AUTH_BASIC_USER_FILE/USR/LOCAL/NGINX/CONF/HTPASSWD;
}
}
----------------------------------------------------------------
Yum Install-y httpd
Htpasswd-c/usr/local/nginx/conf/htpasswd aming//-c created
/USR/LOCAL/NGINX/SBIN/NGINX-T//Check configuration for errors
/usr/local/nginx/sbin/nginx-s Reload//Reload
Curl-x127.0.0.1:80 Test.com-i
curl-uaming:lishiming-x127.0.0.1:80 test.com//404 Error,-u designated user;
Ls/data/wwwroot/test.com
Mkdir/data/wwwroot/test.com
echo "test.com" >/data/wwwroot/test.com/index.html
curl-uaming:lishiming-x127.0.0.1:80 test.com
Specify Access Admin authentication only
Curl-uaming:lishiming-x127.0.0.1:80 test.com/admin/
Vim/usr/local/nginx/conf/vhost/test.com.conf
Location /Modify to location/admin/;
curl-x127.0.0.1:80 test.com //The user is not specified;
CURL-X127.0.0.1:80 test.com/admin///prompt 401 user request
12.9 Nginx Domain Redirection
vim/usr/local/nginx/conf/vhost/test.com.conf//Modify to the following content
----------------------------------------------------------------
Server
{
Listen 80;
server_name test.com test1.com test2.com;
Index index.html index.htm index.php;
root/data/wwwroot/test.com;
if ($host! = ' test.com ') {
Rewrite ^/(. *) $ http://test.com/$1 permanent;
}
Location ~ admin.php
{auth_basic "auth";
AUTH_BASIC_USER_FILE/USR/LOCAL/NGINX/CONF/HTPASSWD;
}
}
----------------------------------------------------------------
^ denotes the beginning of xxx, which refers to HTTP//$host
(. *) indicates a wildcard of all, $ represents until the end;
/USR/LOCAL/NGINX/SBIN/NGINX-T//Check configuration for errors
/usr/local/nginx/sbin/nginx-s Reload//Reload
CURL-X127.0.0.1:80 test2.com/index.html-i//Hint 301, redirected to test.com/index.html Web page;
Extended
nginx.conf Configuration Detailed http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880
Nginx rewrite four kinds of flag http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943
Linux study notes 12 weeks two lessons (April 24)