12.6-12.9 Nginx安装,默认虚拟主机,用户认证,域名重定向
12.6 Nginx安装
大纲
1 进入src目录,把nginx下载在此目录
#cd /usr/local/src
#wget http://nginx.org/download/nginx-1.8.0.tar.gz
2 解压压缩包
#tar zxf nginx-1.12.1.tar.gz
3 进行编译,安装
#./configure --prefix=/usr/local/nginx
#make && make install
nginx的核心程序,也可以利用-t去检查状态。
[root@AliKvn usr]# ls /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx
[root@AliKvn usr]# /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配置文件
#vim /etc/init.d/nginx //复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/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_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL } stop() {echo -n $"Stopping $prog: "killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL } reload() {echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL } restart() {stopstart } configtest() {$NGINX_SBIN -c $NGINX_CONF -treturn 0 } case "$1" instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1 esac exit $RETVAL
5 更改文件755权限
#chmod 755 /etc/init.d/nginx
6 添加开机启动服务
#chkconfig --add nginx
#chkconfig nginx on
7 配置Nginx的配置文件
#cd /usr/local/nginx/conf/
#mv nginx.conf nginx.conf.1
#vim nginx.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/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;} } }
配置文件参数解析:
user 定义启动Nginx指哪个用户
worker_processes 2 启动进程有2个子进程
worker_rlimit_nofile 51200 Nginx最多可以打开的文件数51200
use epoll;使用epoll模式
worker_connections 6000进程最多有6000个链接
server部分对应httpd的v-host虚拟主机
server_name 域名
location php 解析php相关参数部分
root 网页路径
!!!!一般监听80端口出错或者不通,大多数是跟server这部分配置参数有密切关系,还有可能httpd也被启动了导致80端口被占用。
8 编辑完成后,检查状态,进程以及端口
[root@AliKvn 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
检查有无错误,尝试启动服务
(注意,如果httpd启动了会导致80端口被占用,所以必须要关闭,否则nginx启动失败)。
失败报错一般为如下内容,
遇到这种情况,需要把httpd关闭掉,然后再继续启动nginx即可。
[root@AliKvn conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ OK ]
#ps aux |grep nginx 检查进程,有2个子进程(两个nobody)
Ss表示父进程,一般父进程user都是root,子进程都是nobody
检查端口,
#netstat -lntp |grep 80
9 测试php解析
建立php文件
#vim /usr/local/nginx/html/1.php
<?php
echo "this is the Nginx test page.";
curl检查php解析测试
[root@AliKvn conf]# curl 127.0.0.1/1.php
this is the Nginx test page.[root@AliKvn conf]#
12.7 Nginx默认虚拟主机
大纲
在Nginx中也有默认虚拟主机,跟httpd类似,第一个被Nginx加载的虚拟主机就是默认主机。
和httpd不同的是,在Nginx中,可以利用default_server来标记默认虚拟主机,如果虚拟主机没被标记默认,则第一个虚拟主机会被选为默认虚拟主机。
相关配置操作
1 修改nginx的配置文件,在里面增加参数include vhost/*.conf
[root@AliKvn conf]#vim /usr/local/nginx/conf/nginx.conf
参数解释:
include vhost/*.conf;
表示支持vhost/所有*.conf的文件,include支持通配使用
2 建立vhost目录
[root@AliKvn conf]# mkdir vhost
[root@AliKvn 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; }
解析
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;//站点名字
index index.html index.htm index.php;//相关索引页
root /data/wwwroot/default;//站点目录
}
3 建立default目录,并进入,定义新html,创建索引页index.html
[root@AliKvn vhost]# mkdir -p /data/wwwroot/default/
[root@AliKvn vhost]# echo “This is a default site.”>/data/wwwroot/default/index.html
-t检查状态
[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -t
-s重新加载,这里相当于apache的graceful用法
[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload
4 测试
测试默认站点
[root@AliKvn vhost]# curl localhost
this is a default site.
测试默认主机
[root@AliKvn vhost]# curl -x127.0.0.1:80 aaa.com
this is a default site.
[root@AliKvn vhost]# curl -x127.0.0.1:80 aaa1111.com
this is a default site.
12.8 Nginx用户认证
大纲
[root@AliKvn vhost]# cd /usr/local/nginx/conf/vhost
[root@AliKvn vhost]#vim /usr/local/nginx/conf/vhost/test.com.conf
1 写入如下内容,建立认证参数(其实认证参数都是一样的,只是个别参数不同,例如:server name root.其他致一样)
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; } }
解析
auth_basic 用来定义用户认证的名称
auth_basic_user_file 用来定义用户认证的文件以及密码,此处一般接密码文件
2 使用htpasswd工具生成密码(如果没有htpasswd密码工具的话,可以安装httpd yum install -y httpd)
用法,生成文件 指定用户
[root@AliKvn vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming
第二个用户建立****(注意-c是生成建立,如果建立一次后,就不要再用-c去创建,否则会清零然后重置密码)
[root@AliKvn vhost]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd user1
3 检查并重新加载, -t && -s reload
[root@AliKvn 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@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload
4 curl测试,状态码是401未认证。
curl指定用户测试
如果想访问test.com/下面目录的站点,可以做这样的配置
访问/test.com/admin/认证
1 修改location后面的路径,定义为
location /admin/{auth_basic "Auth";auth_basic_user_file /usr/local/nginx/conf/htpasswd; }
2 检查并重新加载, -t && -s reload
3 curl测试
#curl -uaming:passwd -x127.0.0.1 test.com/admin/
访问/test.com/admin.php认证 提示认证 状态码401
curl -uaming:passwd -x127.0.0.1 test.com/admin.php
12.9 Nginx域名重定向
Nginx的域名重定向和httpd类似,但更容易理解,操作如下:
1 修改配置文件test.com.conf
server {listen 80;server_name test.com test1.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;} }
参数解析:
rewrite ^/(.*)$ http://test.com/$1 permanent;
^/(.*)$其实就等于http://$host/(.*)$ ,那么http://$host/ ,相当于http://test.com/
/(.*)$ /表示站点下面的子页,(.*)$表示通配后面的字符串,直到最后,$表示最后。
$1 = (.*)$
$host/可以省略,直接变成^/,意思是以xxx开头,
permanent表示301
!!!!
在Nginx里,server_name后面支持写多个域名。但在httpd里,httpd只能带一个server name,多个域名只能用Alias Name来定义。
permanent为永久重定向,状态码为301,如果写redirect则为302
2 检查配置语法并重新加载 -t&&-s reload
[root@AliKvn 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@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload
3 curl测试
先访问以下主域名test.com 状态码200,表示通过访问
访问test1.com ,即非test.com。访问代码301,这是因为^参数起了作用了。
访问test2.com/111/111,状态码为301,^/(.*)$条件成立。
转载于:https://blog.51cto.com/13578154/2107156
12.6-12.9 Nginx安装,默认虚拟主机,用户认证,域名重定向相关推荐
- 11-4 12 Nginx安装 默认虚拟主机 用户认证 域名重定向
2019独角兽企业重金招聘Python工程师标准>>> 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向 扩展 ngin ...
- Nginx——安装与虚拟主机配置(域名非80端口问题)
一.引言 因为甲方提出问题:一个服务器与IP,两个域名a.b分别对应两个要部署的网站,这个一般是一个域名a对应一个主机服务器ip的默认80端口,另外的网站只能用其他端口(如b:8080对应主机ip:8 ...
- Nginx安装与虚拟主机配置shell脚本
今天继续给大家介绍Linux运维相关知识,本文主要内容是Nginx安装与虚拟主机配置shell脚本. 一.NGINX安装 在今天的脚本上,我们开始尝试使用函数和参数,以增强脚本的灵活性和实现脚本的模块 ...
- Nginx安装、默认虚拟主机、Nginx用户认证、Nginx域名重定向
12.6 Nginx安装 安装包下载到/usr/local/src目录 [root@taoyuan ~]# cd /usr/local/src [root@taoyuan src]# wget htt ...
- linux基于域名的虚拟主机,Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机...
Nginx支持的虚拟主机有三种 ●基于域名的虚拟主机 ●基于IP的虚拟主机 ●基于端口的虚拟主机 每一种虚拟主机均可通过"server{}" 配置段实现各自的功能 基于域名的虚拟主 ...
- Nginx优化之虚拟主机
文章目录 前言 一:Nginx服务基础 1.1:Nginx概述 1.2:Nginx编译安装(过程) 1.3:运行控制(实验过程) 1.4:配置文件nginx.conf 1.5:Nginx的访问状态统计 ...
- 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx
12.6 Nginx安装 [root@martin001 conf]# chkconfig --add nginx [root@martin001 conf]# chkconfig nginx on ...
- linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍
Nginx介绍 Nginx官网(http://nginx.org),最新版1.13,最新稳定版1.12 Nginx应用场景:web服务.反向代理.负载均衡 Nginx著名分支,淘宝基于Nginx开发的 ...
- php编译安装, 编译安装nginx, yum安装nginx, nginx虚拟主机,默认虚拟主机
编译安装PHP-7.3.0 1. 下载程序 下载php 7.3.0 cd /usr/local/src wget http://cn2.php.net/distributions/php-7.3.0. ...
最新文章
- 用户态程序调用系统态程序-快速系统调用
- 蛋花花简单阐述HTML5和Web前端的区别
- Python编程语言学习:包导入和模块搜索路径简介、使用方法之详细攻略
- 【转】NAST表的使用
- JAXB和XStream比较
- String与StringBuffer、StringBuilder之间的转换
- 浏览器测试基本跑分网站
- Python 圈精选文章
- Python 玩出花了!一文教你用 Python 制作吃豆人游戏! | 附代码
- 《Linux Shell脚本攻略》读书笔记第三章 以文件之名
- 黑马程序员-Java基础,Java集合Collection和Iterator接口
- POJ 3083 Children of the Candy Corn(DFS + BFS)
- 一般纳税人税额计算_一般纳税人税率公式是什么样的,税额是怎么计算的-企业纳税|华律办事直通车...
- 某市有甲、乙、丙、丁四个居民区,自来水有A,B,C三个水库供应
- java字符串替换空格_特殊的空格(Java无法替换字符串中的空格)
- Python爬虫爬取B站封面图片,这才是我们学好爬虫的动力!
- webfreer下载及设置
- npm包rimraf介绍
- printf(%d%d%d%d\n, a,b,c);
- 淘宝宝贝详情页的优化技巧