12.6 Nginx Installation
- Cd/usr/local/src
- wget http://nginx.org/download/nginx-1.12.1.tar.gz
- Tar zxvf nginx-1.12.1.tar.gz
- CD nginx-1.12.1
- ./configure--prefix=/usr/local/nginx
- Make && make install
- Vim/etc/init.d/nginx #Create nginx configuration file, copy the following:
---------------------------------------------------------------------------
#!/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
------------------------------------------------------------------------------------
#以上代码参考:
Https://coding.net/u/aminglinux/p/aminglinuxbook/git/blob/master/D15Z/etc_init.d_nginx
--------------------------------------------------------------------------------------------------------------- ---------
- 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//Creating an Nginx Startup script configuration file, write the following:
--------------------------------------------------------------------------------------------------------------- --------
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;
}
}
}
--------------------------------------------------------------------------------------------------------------- ------------
#以上代码参考:
Https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
--------------------------------------------------------------------------------------------------------------- ------------
- /USR/LOCAL/NGINX/SBIN/NGINX-T//Check for syntax errors
- /etc/init.d/nginx Start//Startup Nginx Service
- NETSTAT-LNTP |grep 80//View listening 80 port status
- Test PHP parsing
vi/usr/local/nginx/html/1.php//Add the following:
<?php
echo "Test PHP scripts.";
?>
Curl localhost/1.php//test can parse PHP
- Problems encountered during installation:
[Email protected] nginx-1.12.1]# chkconfig--add nginx
Service Nginx does not support Chkconfig
The reason is that there is an error in the Nginx configuration file, correct as soon as OK.
12.7 Default Virtual Host
- Vim/usr/local/nginx/conf/nginx.conf
- Comment out this part of the server{} and add the following line:
- Include vhost/*.conf;
- Mkdir/usr/local/nginx/conf/vhost//Create a virtual host directory
- Cd/usr/local/nginx/conf/vhos
- Vim default.conf//Add the following:
Server
{
Listen default_server; #有这个标记的就是默认虚拟主机
server_name aaa.com;
Index index.html index.htm index.php;
Root/data/wwwroot/default;
}
- Mkdir-p/data/wwwroot/default/
- echo "This is a default site." >/data/wwwroot/default/index.html
- /usr/local/nginx/sbin/nginx-t
- /usr/local/nginx/sbin/nginx-s Reload #重新加载nginx
To test the default virtual host:
- Curl localhost
- Curl-x127.0.0.1:80 123.com
12.8 Nginx user authentication
- 1. User authentication for the entire site:
Vim/usr/local/nginx/conf/vhost/test.com.conf #写入如下内容
Server
{
Listen 80;
server_name test.com;
Index index.html index.htm index.php;
root/data/wwwroot/test.com;
Location/# user authentication related configuration
{
Auth_basic "Auth";
AUTH_BASIC_USER_FILE/USR/LOCAL/NGINX/CONF/HTPASSWD;
}
}
- Yum install-y httpd #安装apache, if not, you can skip the step
- /usr/local/apache2.4/bin/htpasswd-c/usr/local/nginx/conf/htpasswd Auth_User #指定用户认证的用户名
- /usr/local/nginx/sbin/nginx-t
- /usr/local/nginx/sbin/nginx-s Reload #重新加载nginx
- Mkdir/data/wwwroot/test.com
- echo "test.com" >/data/wwwroot/test.com/index.html
Test user authentication:
(1) Pass Curl Test
curl-x127.0.0.1:80 test.com-i #状态码为401说明需要验证
curl-uauth_user:123-x127.0.0.1:80 test.com-i #访问状态码变为200
(2) test access via browser
Edit the Hosts file for Windows and then access the test.com in the browser to enter the user, password pop-up window
2. User authentication for the directory
location/admin/
{
Auth_basic "Auth";
AUTH_BASIC_USER_FILE/USR/LOCAL/NGINX/CONF/HTPASSWD;
}
- 3. User authentication for a page
Location ~ admin.php
{
Auth_basic "Auth";
AUTH_BASIC_USER_FILE/USR/LOCAL/NGINX/CONF/HTPASSWD;
}
Knowledge Points:
/usr/local/nginx/sbin/nginx-s Reload
Reloading, if the configuration file has a syntax error, it does not take effect, so that does not cause the Nginx service to stop, and restart the word/usr/local/nginx/sbin/nginx restart may cause the Nginx service due to errors and can not start normally.
12.9 Nginx Domain Redirection
- Edit/usr/local/nginx/conf/vhost/test.com.conf as follows:
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;
}
}
- Test results:
Knowledge Points:
- server_name support to write multiple domain names, which is different from httpd, httpd can only be done by the server alias to do this.
- Permanent is permanently redirected, the status code is 301, and if written as redirect, the status code is 302.
Extended Learning:
NGINX.CONF Configuration Detailed
Http://www.ha97.com/5194.html
http://my.oschina.net/duxuefeng/blog/34880
Nginx rewrite four kinds of flag
Http://www.netingcn.com/nginx-rewrite-flag.html
http://unixman.blog.51cto.com/10163040/1711943
2018-3-13 Linux Learning Notes