Nginx安装、默认虚拟主机、Nginx用户认证、Nginx域名重定向

Nginx安装

  1. 进入下载安装包目录javascript

    cd /usr/local/src
  2. 下载nginx安装包php

    wget http://nginx.org/download/nginx-1.15.3.tar.gz
  3. 解包解压nginx安装包css

    tar -zxvf nginx-1.15.3.tar.gz
  4. 进入解压nginx目录下html

    cd nginx-1.15.3.tar.gz
  5. ./configure nginx 完成该操做后执行echo $? 验证是否有错java

    ./configure --prefix=/usr/local/nginx
  6. make和make install 编译安装node

    [root[@localhost](https://my.oschina.net/u/570656) ~]# make && make install
  7. 在/etc/init.d/目录下,建立nginx启动脚本,并写入内容linux

    [root[@localhost](https://my.oschina.net/u/570656) ~]#  vim /etc/init.d/nginx

写入内容参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx 内容以下: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
  1. 给nginx(启动脚本) 设定755权限git

    [root[@localhost](https://my.oschina.net/u/570656) ~]# chmod 755 /etc/init.d/nginx
  2. 添加 nginx服务 到服务列表apache

    [root[@localhost](https://my.oschina.net/u/570656) ~]# chkconfig --add nginx
  3. 设定 nginx服务 开机启动

    [root[@localhost](https://my.oschina.net/u/570656) ~]# chkconfig nginx on
  4. 进入 /usr/local/nginx/conf/

  5. 把...conf目录下的nginx.conf文件重命名

    [root@localhost conf]# mv nginx.conf nginx.conf.1
  6. 在...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_pass 127.0.0.1:9000;
    
     fastcgi_index index.php;
    
     fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    
     }
    
     }
    
     }
  7. 检测nginx配置文件是否有错

    [root@localhost conf]# /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
  8. 启动nginx服务

    [root@localhost conf]# /etc/init.d/nginx start
     Starting nginx (via systemctl):                            [  肯定  ]
  9. 建立一个1.php测试文件

    vim /usr/local/nginx/html/1.php
  10. 使用curl命令测试

    [root@localhost conf]# curl 127.0.0.1/1.php
     welcome to nginx[root@localhost conf]# vim /usr/local/nginx/html/1.php

能够看到PHP已成功解析。

Nginx默认虚拟主机

  1. 编辑nginx.conf配置文件

    [root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
  2. 删除下面所示的内容

    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_pass 127.0.0.1:9000;
    
             fastcgi_index index.php;
    
             fastcgi_param SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
    
         }
    
     }
  3. 写入一行内容 include vhost/*.conf;

    pplication/xml;
    
     include vhost/*.conf;
     }
  4. 在当前目录下,建立vhost目录

    [root@localhost conf]# mkdir /usr/local/nginx/conf/vhost
  5. 进入/usr/local/nginx/conf/vhost目录下

  6. 建立一个aaa.com.conf文件,并写入内容

    [root@localhost conf]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
     server
     {
     listen 80 default_server;              #default_server表示默认虚拟主机
     server_name aaa.com;
     index index.html index.htm index.php;
     root /data/wwwroot/default;
     }
  7. 建立default目录

    [root@localhost vhost]# mkdir -p /data/wwwroot/default
  8. 进入/data/wwwroot/default目录

    [root@localhost vhost]# cd /data/wwwroot/default/
  9. 建立一个index.html文件,并写入内容:

    [root@localhost default]# vim index.html
     [root@localhost default]# cat index.html 
     This is a test page for nginx
  10. 检测nginx配置文件是否有错

    [root@localhost default]# /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
  11. 从新加载Nginx

    [root@localhost default]# /usr/local/nginx/sbin/nginx -s reload
  12. 使用curl测试

    [root@localhost default]# curl -x127.0.0.1:80 www.hello.com
     This is a test page for nginx
     [root@localhost default]# curl -x127.0.0.1:80 aaa.com
     This is a test page for nginx

能够看到测试结果,不管使用什么域名,只要解析到该主机上就会默认访问该站点,这就是默认虚拟主机的特权。

Nginx用户认证

网站主页面(根目录)访问受限(需用户认证)

  1. 进入/usr/local/nginx/conf/vhost目录下

  2. 建立一个test.com.conf文件,并写入内容

    [root@localhost vhost]# vim 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;
     }
     }
  3. 在/data/wwwroot/test.com目录下建立一个index.html的文件

    在Windows上作一个地址解析,而后经过网页访问

说明用户认证已经配置成功

  1. 建立认证用户和密码 ,须要有apache的支持

    yum install httpd -y
  2. 使用htpasswd命令给Nginx建立用户密码须要绝对路径,第二次再建立用户时就不能加-c了,若是再加-c会把以前的帐户都覆盖掉。

    [root@localhost vhost]# /usr/local/apache/bin/htpasswd -c /usr/local/nginx/conf/htpasswd lisi
     New password: 
     Re-type new password: 
     Adding password for user lisi
     [root@localhost vhost]# cat /usr/local/nginx/conf/htpasswd 
     lisi:$apr1$a3bgoFwN$ubHbITBBewSAU0XJd4/nT.
  3. 从新加载一下Nginx

    [root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
  4. 验证

针对目录作用户认证

  1. 编辑配置文件

    [root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 将location / 改成location /admin/

  3. 从新加载

    [root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
  4. 在/data/wwwroot/test.com/下建立admin/目录

    [root@localhost vhost]# curl -x127.0.0.1:80 test.com/admin/
     <html>
     <head><title>401 Authorization Required</title></head>
     <body bgcolor="white">
     <center><h1>401 Authorization Required</h1></center>
     <hr><center>nginx/1.15.3</center>
     </body>
     </html>
     [root@localhost vhost]# curl -ulisi:lichao@123 -x127.0.0.1:80 		test.com/admin/
     zhendui mulu zuo yonghu renzheng
  5. 一样,若是是针对单个页面须要作认证的话,就在location后面写文件名就能够了。

Nginx域名重定向

  1. 进入vhost目录

    [root@localhost vhost]# cd /usr/local/nginx/conf/vhost
  2. 说明

    在Nginx里“server_name” 支持跟多个域名;可是Apache“server_name”只能跟一个域名,须要跟多个域名,须要使用Alisa;
     在Nginx的conf配置文件里“server_name ” 设置了多个域名,就会使网站的权重变了,到底须要哪一个域名为主站点呢
     因此须要域名重定向
  3. 编辑配置文件

    server
     {
      listen 80 ;
       server_name test.com;
       index index.html index.htm index.php;
       root /data/wwwroot/test.com;
       if ($host != ‘test.com’ )
       {
       rewrite  ^/(.*)$  http://test.com/$1  permanent;
       }
     }

下面这一段是说明

server
	{
	   listen 80 ;
	  server_name test.com;
	  index index.html index.htm index.php;
	  root /data/wwwroot/test.com;
	 if ($host!= ‘test.com’ )   //假如域名,“!=”不等于 test.com,将执行下面的脚本
	  {
 		   rewrite  ^/(.*)$  http://test.com/$1  permanent;   //   ^/(.*)$  正式写法  http://$host/(.*)$ 这段能够直接省略掉的,同时还能够加上一些规则,“ permanent ”就是301的意思;若是想弄成302,只须要更改成“ redirect ”
	    }

	}
  1. 使用curl测试

    [root@localhost vhost]# curl -x127.0.0.1:80 test1.com -I
     HTTP/1.1 200 OK
     Server: nginx/1.15.3
     Date: Mon, 03 Sep 2018 19:16:05 GMT
     Content-Type: text/html
     Content-Length: 30
     Last-Modified: Mon, 03 Sep 2018 17:30:36 GMT
     Connection: keep-alive
     ETag: "5b8d6fbc-1e"
     Accept-Ranges: bytes

能够看到,原本不存在的test1.com成功的重定向到了test.com

相关文章
相关标签/搜索