6月7日任务

12.6 Nginx安装
12.7 默认虚拟主机
12.8 Nginx用户认证
12.9 Nginx域名重定向

扩展
nginx.conf 配置详解 http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880
nginx rewrite四种flag http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943

12.6 Nginx安装

安装步骤如下:

1、进入SRC目录并使用wget下载Nginx源码包

[root@jimmylinux- ~]# cd /usr/local/src

[root@jimmylinux- src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz

[root@jimmylinux- src]# ls
mysql-5.6.-linux-glibc2.-x86_64.tar.gz nginx-1.12..tar.gz php-5.6. php-5.6..tar.gz

2、解压源码包

[root@jimmylinux- src]# tar zxf nginx-1.12..tar.gz  解压源码包

3、进入到Nginx目录,然后配置文件。

[root@jimmylinux- src]# cd nginx-1.12.

[root@jimmylinux-001 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx

4、编译及编译安装

[root@jimmylinux- nginx-1.12.]# make && make install
[root@jimmylinux- nginx-1.12.]# ls /usr/local/nginx
conf html logs sbin [root@jimmylinux- nginx-1.12.]# ls /usr/local/nginx/conf 配置文件目录
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default [root@jimmylinux- nginx-1.12.]# ls /usr/local/nginx/html 样例文件
50x.html index.html [root@jimmylinux- nginx-1.12.]# ls /usr/local/nginx/logs 日志目录 [root@jimmylinux- nginx-1.12.]# ls /usr/local/nginx/sbin/ 核心进程文件
nginx

[root@jimmylinux-001 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t  -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

5、创建配置文件做一个启动脚本

[root@jimmylinux- nginx-1.12.]# vim /etc/init.d/nginx

添加以下内容

#!/bin/bash
# chkconfig: -
# 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=
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
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=
esac
exit $RETVAL

6、修改权限、把Nginx添加到服务列表、设置开机自启动。

[root@jimmylinux- nginx-1.12.]# chmod  /etc/init.d/nginx  修改权限

[root@jimmylinux- nginx-1.12.]# chkconfig --add nginx  添加到服务列表

[root@jimmylinux- nginx-1.12.]# chkconfig nginx on  设置开机自启动

7、配置(编辑)配置文件

[root@jimmylinux- nginx-1.12.]# cd /usr/local/nginx/conf/

[root@jimmylinux- conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default [root@jimmylinux- conf]# mv nginx.conf nginx.cof.bak 备份自带的Nginx.conf文件 [root@jimmylinux- conf]# vim nginx.conf 新建并编辑配置文件 添加以下内容 user nobody nobody; 指定运行用户
worker_processes ; 指定子进程数量
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile ;
events
{
use epoll;
worker_connections ; 指定进程最大连接数
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size ;
server_names_hash_max_size ;
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 ;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size ;
client_header_buffer_size 1k;
large_client_header_buffers 4k;
request_pool_size 4k;
output_buffers 32k;
postpone_output ;
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 8k;
gzip_comp_level ;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server 定义每一个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;
}
}
}

8、启动Nginx服务

[root@jimmylinux- 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 [root@jimmylinux- conf]# /etc/init.d/nginx start 启动Nginx
Starting nginx (via systemctl): [ 确定 ] [root@jimmylinux- conf]# ps aux |grep nginx 查看Nginx服务进程,有小写字母s表示有父进程。
root 0.0 0.0 ? Ss : : nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 0.0 0.0 ? S : : nginx: worker process
nobody 0.0 0.0 ? S : : nginx: worker process
root 0.0 0.0 pts/ S+ : : grep --color=auto nginx

Nginx配置完成后localhost测试一下

访问IP地址也同样可以显示欢迎界面

Nginx同样也支持PHP解析

[root@jimmylinux- conf]# vim /usr/local/nginx/html/.php  新建1.php文件

加入以下内容

<?php
  echo "This is Nginx test php parsing page.";
  ?>

12.7 默认虚拟主机

1、编辑Nginx配置文件nginx.conf

[root@jimmylinux- conf]# vim nginx.conf

在http最下面的地方加入以下内容

include vhost/*.conf

2、创建子目录并新建配置文件

[root@jimmylinux- conf]# mkdir vhost  创建子目录

[root@jimmylinux- conf]# cd vhost/

[root@jimmylinux- vhost]# vim aaa.com.conf  新建配置文件

加入以下内容

server
{
listen default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}

3、创建目录并新建一个index.html文件

[root@jimmylinux- vhost]# mkdir -p /data/wwwroot/default/

[root@jimmylinux- vhost]# cd /data/wwwroot/default/

[root@jimmylinux- default]# vim index.html

加入以下内容

This is the default site.
[root@jimmylinux- 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

4、重启Nginx服务

[root@jimmylinux- default]# /etc/init.d/nginx restart  重启Nginx服务或重新加载都行

[root@jimmylinux- default]# /usr/local/nginx/sbin/nginx -s reload  重新加载配置文件

5、curl命令访问测试

[root@jimmylinux- default]# curl localhost
This is the default site. [root@jimmylinux- default]# curl 127.0.0.1
This is the default site. [root@jimmylinux- default]# curl -x127.0.0.: aaa.com
This is the default site.

12.8 Nginx用户认证

1、创建一个虚拟主机

[root@jimmylinux- conf]# cd vhost/

[root@jimmylinux- vhost]# vim test.com.conf  新建配置文件

增加以下内容

server
{
listen ;
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; 用户名密码文件
}
}

2、生成密码文件,可以使用Apache自带的htpasswd工具,如果没有就yum安装后再生成。

[root@jimmylinux- vhost]# yum install -y httpd

[root@jimmylinux- vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd jimmy  -c表示创建用户名为jimmy
New password: 输入密码
Re-type new password: 再次输入相同的密码
Adding password for user jimmy 生成用户jimmy对应的密码 [root@jimmylinux- vhost]# cat /usr/local/nginx/conf/htpasswd 查看密码
jimmy:$apr1$VGQQYHsB$8hpeYxjew5BwrzuGJ5APc/ [root@jimmylinux- vhost]# htpasswd /usr/local/nginx/conf/htpasswd user1 如果创建第二个用户,这个时候不需要加-c
New password:
Re-type new password:
Adding password for user user1 [root@jimmylinux- vhost]# !cat
cat /usr/local/nginx/conf/htpasswd
jimmy:$apr1$VGQQYHsB$8hpeYxjew5BwrzuGJ5APc/
user1:$apr1$yN8V9Tv/$M9H.YcEMHMUR4pD6vl//d/

3、测试配置并重新加载

[root@jimmylinux- vhost]# /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@jimmylinux- vhost]# /usr/local/nginx/sbin/nginx -s reload
重新加载的好处,当你配置文件有问题时,重新加载是不会生效的,不会破坏原来Nginx的服务,万一配置文件有问题,一重启服务停了,那问题就大了,所以还是使用-s加载比较好。 [root@jimmylinux- vhost]# curl -x127.0.0.: test.com
<html>
<head><title> Authorization Required</title></head> 401报错说明没有指定用户,需要指定用户验证。
<body bgcolor="white">
<center><h1> Authorization Required</h1></center>
<hr><center>nginx/1.12.</center>
</body>
</html> [root@jimmylinux- vhost]# curl -x127.0.0.: test.com -I
HTTP/1.1 Unauthorized
Server: nginx/1.12.
Date: Thu, Jun :: GMT
Content-Type: text/html
Content-Length:
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@jimmylinux- vhost]# curl -ujimmy:abcd1234 -x127.0.0.: test.com
<html>
<head><title> Not Found</title></head> 404报错,因为访问的index还没有创建,所以会提示404。
<body bgcolor="white">
<center><h1> Not Found</h1></center>
<hr><center>nginx/1.12.</center>
</body>
</html>

[root@jimmylinux-001 vhost]# mkdir /data/wwwroot/test.com/  创建目录

[root@jimmylinux-001 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html 指定index.html文件

[root@jimmylinux-001 vhost]# curl -ujimmy:abcd1234 -x127.0.0.1:80 test.com  重新测试就可以访问了
 test.com

这种认证是针对整个站点进行认证

如果需要对目录进行认证,编辑配置文件指定目录即可。

[root@jimmylinux-001 vhost]# vim test.com.conf

[root@jimmylinux- vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@jimmylinux- vhost]# curl -x127.0.0.: test.com
test.com

[root@jimmylinux- vhost]# curl -x127.0.0.: test.com/admin/
<html>
<head><title> Authorization Required</title></head>
<body bgcolor="white">
<center><h1> Authorization Required</h1></center>
<hr><center>nginx/1.12.</center>
</body>
</html>

[root@jimmylinux- vhost]# mkdir /data/wwwroot/test.com/admin 创建目录

[root@jimmylinux- vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html 创建测试页面 [root@jimmylinux- vhost]# curl -x127.0.0.: test.com/admin/
<html>
<head><title> Authorization Required</title></head>
<body bgcolor="white">
<center><h1> Authorization Required</h1></center>
<hr><center>nginx/1.12.</center>
</body>
</html>

[root@jimmylinux- vhost]# curl -ujimmy:abcd1234 -x127.0.0.: test.com/admin/ 指定用户名和密码,访问成功。
test.com admin dir

还可以针对URL匹配指定的访问路径,同样是编辑配置文件。

[root@jimmylinux- vhost]# vim test.com.conf

server
{
listen ;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com; location ~ admin.php 匹配php的访问路径
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}

12.9 Nginx域名重定向

1、更改配置文件test.com.conf

[root@jimmylinux- vhost]# vim test.com.conf

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

2、curl测试

[root@jimmylinux- vhost]# /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@jimmylinux- vhost]# /usr/local/nginx/sbin/nginx -s reload [root@jimmylinux- vhost]# curl -x127.0.0.: test2.com/index.html -I
HTTP/1.1 Moved Permanently
Server: nginx/1.12.
Date: Thu, Jun :: GMT
Content-Type: text/html
Content-Length:
Connection: keep-alive
Location: http://test.com/index.html [root@jimmylinux- vhost]# curl -x127.0.0.: test2.com/admin/index.html -I
HTTP/1.1 Moved Permanently
Server: nginx/1.12.
Date: Thu, Jun :: GMT
Content-Type: text/html
Content-Length:
Connection: keep-alive
Location: http://test.com/admin/index.html [root@jimmylinux- vhost]# curl -x127.0.0.: test3.com/admin/index.html/adjlfj -I
HTTP/1.1 Moved Permanently
Server: nginx/1.12.
Date: Thu, Jun :: GMT
Content-Type: text/html
Content-Length:
Connection: keep-alive
Location: http://test.com/admin/index.html/adjlfj [root@jimmylinux- vhost]# curl -x127.0.0.: test4.com/admin/index.html/adjlfj -I
HTTP/1.1 Not Found
Server: nginx/1.12.
Date: Thu, Jun :: GMT
Content-Type: text/html
Content-Length:
Connection: keep-alive

Nginx安装、默认虚拟主机、Nginx用户认证和域名重定向的更多相关文章

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

    Nginx安装 cd /usr/local/src (http://nginx.org/en/download.html) wget http://nginx.org/download/nginx-1 ...

  2. nginx创建默认虚拟主机

    创建默认虚拟主机配置文件作用:禁止任何人通过ip或未允许的域名访问web服务. 如:vim vhosts/default.conf server { listen 80 default; server ...

  3. LNMP配置——Nginx配置 —— 默认虚拟主机

    一.配置 首先修改配置文件 #vi /usr/local/nginx/conf/nginx.conf 在最后一个结束符号}前加一行配置: include vhost/*.conf; 意思就是/usr/ ...

  4. LAMP 1.8默认虚拟主机

    默认虚拟主机是为了解决别人域名恶心绑定自己的服务器ip,可导致服务器上的网站排名靠后,即干扰seo优化 我们访问指定的两个网站可以直接访问,ip也可以访问 打开配置文件 vim /usr/local/ ...

  5. apache禁止默认虚拟主机

    禁止默认虚拟主机:作用使除特定域名外,其它的域名/ip无法访问此站点. 在虚拟主机配置文件中 即:/usr/local/apache2/conf/extra/httpd-vhosts.conf 将其中 ...

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

    1.nginx:官网:www.nginx.org 下载:wget -c  http://nginx.org/download/nginx-1.14.0.tar.gz解压:tar -zxvf nginx ...

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

    一. Nginx安装 cd /usr/local/src wget http://nginx.org/download/nginx-1.12.1.tar.gz 版本在http://nginx.org/ ...

  8. [转] linux学习第四十四篇:Nginx安装,Nginx默认虚拟主机,Nginx域名重定向

    Nginx安装 进入存放源码包的目录: cd /usr/local/src 下载源码包: wget http://nginx.org/download/nginx-1.12.1.tar.gz 解压: ...

  9. centos LAMP第二部分apache配置 下载discuz!配置第一个虚拟主机 安装Discuz! 用户认证 配置域名跳转 配置apache的访问日志 配置静态文件缓存 配置防盗链 访问控制 apache rewrite 配置开机启动apache tcpdump 第二十节课

    centos    LAMP第二部分apache配置  下载discuz!配置第一个虚拟主机 安装Discuz! 用户认证 配置域名跳转  配置apache的访问日志  配置静态文件缓存  配置防盗链 ...

随机推荐

  1. 在虚拟机上的关于FTP FTP访问模式(虚拟用户模式)

    首先你要有vsftpd服务 可以先去yum中下载(当然你要有本地yum仓库) 输入命令: yum  install  vsftpd 下载完成之后打开vsftpd服务 输入命令:systemctl   ...

  2. [考试反思]0901NOIP模拟测试34:游离

    又是放假回来的收心考.幸而熬了夜回来也不至于很困(虽说第二天早上困成狗...) 说分数吧: skyhAK300,后面有220,220,220,190,190,180 我170,排第8.凑合,其实不太满 ...

  3. docker已运行容器添加或修改端口映射

    # 不推荐方法:将原来的容器提交成镜像,然后利用新的建立的镜像重新建立一个带有端口映射的容器# 推荐方法:## 查看id 就是 容器的 hash_of_the_container 数值 docker ...

  4. NOIP模(ka)拟(chang)测试30 考试报告

    应得分:300 实得分:210 毒瘤卡常出题人,卡掉90分! T1 Return 开个副本数组sort一下,unique去重就可以啦.时间复杂度$ O(nlog2(n)) $ T2 One 其实就是约 ...

  5. MinIO 参数解析与限制

    MinIO 参数解析与限制 MinIO server 在默认情况下会将所有配置信息存到 ${HOME}/.minio/config.json 文件中. 以下部分提供每个字段的详细说明以及如何自定义它们 ...

  6. 简单Web UI 自动化测试框架 pyse

    WebUI automation testing framework based on Selenium and unittest. 基于 selenium 和 unittest 的 Web UI自动 ...

  7. JDBC报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

    报错原因:查阅资料发现这都是因为安装mysql的时候时区设置的不正确 mysql默认的是美国的时区,而我们中国大陆要比他们迟8小时,采用+8:00格式 解决方法: 1.修改MySQL的配置文件,MyS ...

  8. linux No module named yum错误的解决办法

    linux No module named yum错误的解决办法 肯定是yum的版本与当前python的版本不一致造成的 <pre>所以修改yum的配置,修改文件: vim /usr/bi ...

  9. css3的过渡和动画的属性介绍

    一.过渡 什么是过渡? 过渡是指:某元素的css属性值在一段时间内,平滑过渡到另外一个值,过渡主要观察的是过程和结果. 设置能够过渡的属性: 支持过渡的样式属性,颜色的属性,取值为数值,transfo ...

  10. 瞎折腾实录:构建 Armel 版本的 .NET Core 教程和资料资源

    目录 首先我要说明,我失败了~ 我把我的进度和经验放出来,希望能够帮助别人完成编译工作~ 背景:最近接手一个华为某型号的嵌入式设备,需要在上面搭建 .NET Core 环境. 设备是 Armel 架构 ...