6月7日任务

12.6 Nginx安装

安装过程

# cd /usr/local/src

# wget http://nginx.org/download/nginx-1.14.0.tar.gz  (一般来说,偶数版本为稳定版,适合生产环境)

# tar xzf nginx-1.14.0.tar.gz  

# cd nginx-1.14.0/

# ./configure --prefix=/usr/local/nginx

# make && make install

设置过程

# cd /usr/local/nginx/conf/

# mv nginx.conf nginx.conf1   //把系统自带的配置文件改名备份

# vim 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;
}
}
}
# chmod 755  /etc/init.d/nginx

#/etc/init.d/nginx start

# ps aux |grep nginx

6月7日任务

# ps aux |grep php-fpm

6月7日任务


测试

# curl localhost

6月7日任务


#vim /usr/local/nginx/html/1.php  //创建一个php文件,简单写入以下内容

6月7日任务

# curl localhost/1.php   //可以正常解析php

6月7日任务



12.7 Nginx默认虚拟主机

# vim /usr/local/nginx/conf/nginx.conf           //编辑配置文件,删除最后几行并添加一行内容

6月7日任务

# mkdir /usr/local/nginx/conf/vhost

#cd /usr/local/nginx/conf/vhost

# vim aaa.com.conf            //新建并添加如下内容

server
{
    listen 80 default_server;   //有这个标记就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

6月7日任务


# mkdir  -p /data/wwwroot/default

# echo “This is a default site.”>/data/wwwroot/default/index.html

# /usr/local/nginx/sbin/nginx -t    //检查语法

6月7日任务

# /usr/local/nginx/sbin/nginx -s reload  //重新加载


测试

# curl localhost

6月7日任务


下面说一下总结默认虚拟主机的两种方法

1.在/usr/local/nginx/conf/vhost目录下排在第一位,比如下面aaa.com.conf就是默认的虚拟主机

6月7日任务

2.改配置文件

6月7日任务


12.8 Nginx用户认证

#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;
}
}
6月7日任务


生产密码文件,没有httpwd的可以yum进行安装(# yum install -y httpd)

# /usr/local/apache2/bin/htpasswd  -c  /usr/local/nginx/conf/htpasswd xm    //-c 生成密码文件,再次添加用户时要去掉,否则会覆盖掉原来的密码文件,生成xm用户的密码文件,并指定路径

6月7日任务


检查语法并重新加载

# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload   //reload优点是不会影响原来的服务,而restart若配置错误可能会有服务启动不了的情况

6月7日任务


测试

#curl -x127.0.0.1:80 test.com      //不指定用户名密码,代码401

6月7日任务


创建test.com的html文件

# mkdir /data/wwwroot/test.com

# echo "test.com"  >/data/wwwroot/test.com/index.html      


6月7日任务


指定用户名密码访问

# curl -uxm:112358 -x127.0.0.1:80 test.com

6月7日任务


前面是针对整个站点进行用户认证,还可以针对某个目录进行用户认证

1,修改配置文件,添加admin目录

#vim /usr/local/nginx/conf/vhost/test.com.conf

6月7日任务


2.检查语法并重新加载

#/usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

3.测试

# curl  -x127.0.0.1:80 test.com
6月7日任务

# curl  -x127.0.0.1:80 test.com/admin   //401不能访问admin目录

6月7日任务


在admin目录下创建html文件

#mkdir /data/wwwroot/test.com/admin
# echo "admin" > /data/wwwroot/test.com/admin/index.html


指定用户名密码访问admin

#  curl  -uxm:112358 -x127.0.0.1:80 test.com/admin/

6月7日任务


还可以针对特定页面

1.修改配置文件

#vim /usr/local/nginx/conf/vhost/test.com.conf

6月7日任务

2.检查语法并重新加载

3.创建admin.php文件

# echo "admin.php" > /data/wwwroot/test.com/admin.php

4.访问测试

#  curl  -x127.0.0.1:80 test.com/admin.php   //不指定用户名密码,401

6月7日任务

# curl  -uxm:112358 -x127.0.0.1:80 test.com/admin.php    //正常访问

6月7日任务


12.9Nginx域名重定向

1.修改配置文件

#vim /usr/local/nginx/conf/vhost/test.com.conf   //多个域名以空格分割

6月7日任务

sever_name后面支持多个域名,这里和apache不同

permanent为永久重定向,状态码为301,如果写redirect则为302

2.检查语法重新加载

3.测试

#  curl -x127.0.0.1:80 test1.com/admin.php -I   //代码301,配置成功

6月7日任务