Nginx安装
[root@iZbp1e0xboek6oow616aoiZ src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@iZbp1e0xboek6oow616aoiZ src]# tar zxvf nginx-1.14.0.tar.gz
[root@iZbp1e0xboek6oow616aoiZ src]# cd nginx-1.14.0
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# echo $?
0
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# make && make install
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# echo $?
0
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# vim /etc/init.d/nginx
#!/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 "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# chmod 755 /etc/init.d/nginx
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# chkconfig --add nginx
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# chkconfig nginx on
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# vim /usr/local/nginx/conf/nginx.conf
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;
}
}
}
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 确定 ]
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# netstat -lntp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13619/nginx: master
12.7 Nginx默认虚拟主机
[root@iZbp1e0xboek6oow616aoiZ nginx]# vim /usr/local/nginx/conf/nginx.conf
#删除server,并在http中最后添加include vhost/*.conf;
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;
include vhost/*.conf;
}
[root@iZbp1e0xboek6oow616aoiZ nginx]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@iZbp1e0xboek6oow616aoiZ nginx]# mkdir /usr/local/nginx/conf/vhost
[root@iZbp1e0xboek6oow616aoiZ nginx]# vim /usr/local/nginx/conf/vhost/aaa.conf
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
[root@iZbp1e0xboek6oow616aoiZ nginx]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@iZbp1e0xboek6oow616aoiZ nginx]# mkdir -p /data/wwwroot/default
[root@iZbp1e0xboek6oow616aoiZ nginx]# vim /data/wwwroot/default/index.html
test page
[root@iZbp1e0xboek6oow616aoiZ nginx]# /usr/local/nginx/sbin/nginx -s reload
Nginx用户认证
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# 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 /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# yum install -y httpd
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# htpasswd -c /usr/local/nginx/conf/htpasswd test
New password:
Re-type new password:
Adding password for user test
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# mkdir /data/wwwroot/test.com
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# echo “test.com”>/data/wwwroot/test.com/index.html
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.14.0
Date: Tue, 24 Apr 2018 09:10:41 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# curl -u test:123456 -x 127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Tue, 24 Apr 2018 09:11:19 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Tue, 24 Apr 2018 09:10:29 GMT
Connection: keep-alive
ETag: "5adef485-f"
Accept-Ranges: bytes
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
针对目录的用户认证
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# vim /usr/local/nginx/conf/vhost/test.com.conf
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
针对文件匹配的用户认证
[root@iZbp1e0xboek6oow616aoiZ nginx-1.14.0]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
Nginx域名重定向
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;
[root@iZbp1e0xboek6oow616aoiZ test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@iZbp1e0xboek6oow616aoiZ test.com]# curl -x127.0.0.1:80 test1.com/test/admin.php -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Tue, 24 Apr 2018 13:44:33 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/test/admin.php
扩展
nginx.conf 配置详解 http://www.ha97.com/5194.html http://my.oschina.net/duxuefeng/blog/34880
nginx rewrite四种flag http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943