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


# ps aux |grep php-fpm



测试

# curl localhost



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


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




12.7 Nginx默认虚拟主机

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

# 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;
}



# 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  //重新加载


测试

# curl localhost



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

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


2.改配置文件



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;
}
}


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

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




检查语法并重新加载

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


测试

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



创建test.com的html文件

# mkdir /data/wwwroot/test.com

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




指定用户名密码访问

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



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

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

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



2.检查语法并重新加载

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

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

3.测试

# curl  -x127.0.0.1:80 test.com


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


在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/



还可以针对特定页面

1.修改配置文件

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


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


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


12.9Nginx域名重定向

1.修改配置文件

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


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

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

2.检查语法重新加载

3.测试

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




微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码 微信小程序毕业设计期末大作业项目源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值