12.6 Nginx 安装
- [root@localhost ~]# cd /usr/local/src/
- [root@localhost src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
- (过程省略)
- [root@localhost src]# tar zxvf nginx-1.12.2.tar.gz
- [root@localhost src]# cd nginx-1.12.2/
- [root@localhost nginx-1.12.2]# ls
- auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
- [root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx ## 编译 nginx
- (过程省略)
- [root@localhost nginx-1.12.2]# make && make install
- (过程省略)
- [root@localhost nginx-1.12.2]# ls /usr/local/nginx/
- conf html logs sbin
- [root@localhost nginx-1.12.2]# ls /usr/local/nginx/conf/
- fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
- fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
- fastcgi_params koi-win nginx.conf scgi_params.default win-utf
- [root@localhost nginx-1.12.2]# ls /usr/local/nginx/html/
- 50x.html index.html
- [root@localhost nginx-1.12.2]# ls /usr/local/nginx/logs/
- [root@localhost nginx-1.12.2]# ls /usr/local/nginx/sbin/
- nginx
- [root@localhost nginx-1.12.2]# /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@localhost nginx-1.12.2]# vim /etc/init.d/nginx
- [root@localhost nginx-1.12.2]# chmod 755 !$
- chmod 755 /etc/init.d/nginx
- [root@localhost nginx-1.12.2]# chkconfig --add nginx
- [root@localhost nginx-1.12.2]# chkconfig nginx on
- [root@localhost nginx-1.12.2]# chkconfig
注: 该输出结果只显示 SysV 服务, 并不包含
原生 systemd 服务 SysV 配置数据
可能被原生 systemd 配置覆盖
要列出 systemd 服务, 请执行 'systemctl list-unit-files'
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'
mysqld 0: 关 1: 关 2: 开 3: 开 4: 开 5: 开 6: 关
netconsole 0: 关 1: 关 2: 关 3: 关 4: 关 5: 关 6: 关
network 0: 关 1: 关 2: 开 3: 开 4: 开 5: 开 6: 关
nginx 0: 关 1: 关 2: 开 3: 开 4: 开 5: 开 6: 关
php-fpm 0: 关 1: 关 2: 开 3: 开 4: 开 5: 开 6: 关
- [root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
- [root@localhost conf]# ls
- fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
- fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
- fastcgi_params koi-win nginx.conf scgi_params.default win-utf
- [root@localhost conf]# mv nginx.conf nginx.conf.bak
- [root@localhost conf]# vim nginx.conf ## 编辑配置文件
user 定义上传等操作完成的用户
worker_processes 定义子进程的数量
error_log 错误日志
pid pid 号
worker_rlimit_nofile 指定 nginx 最多打开多少文件
use epoll 使用 epoll 模式
worker_connections 进程最大的连接数
fastcgi_pass 如果监听端口是 9000, 可以写为 127.0.0.1:9000 这是两种不同方式
- [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
- [root@localhost conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 确定 ]
- [root@localhost conf]# ps aux |grep nginx
- root 9634 0.0 0.0 20496 628 ? Ss 21:26 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- nobody 9635 0.0 0.3 22940 3212 ? S 21:26 0:00 nginx: worker process
- nobody 9636 0.1 0.3 22940 3212 ? S 21:26 0:00 nginx: worker process
- root 9638 0.0 0.0 112676 980 pts/0 S+ 21:26 0:00 grep --color=auto nginx
- (上方有两个子进程 worker process, 由配置文件中的 worker_processes 定义的)
- [root@localhost conf]# curl localhost
- (上方欢迎语由 / usr/local/nginx/html/index.html, 而为什么能访问到 index.html 由 nginx.conf 定义)
- [root@localhost conf]# cd /usr/local/nginx/html/
- [root@localhost html]# vim 1.php
- [root@localhost html]# curl localhost/1.php
- This is nginx test page.[root@localhost html]#
12.7 默认虚拟主机
[root@localhost conf]# vim nginx.conf
删除以上内容, 增加一行 include vhost/*.conf
- [root@localhost conf]# mkdir vhost
- [root@localhost conf]# cd vhost/
- [root@localhost vhost]# vim aaa.com.conf
- (由 default_server 就证明这是默认虚拟主机)
- [root@localhost vhost]# mkdir -p /data/wwwroot/default
- [root@localhost vhost]# cd !$
- cd /data/wwwroot/default
- [root@localhost default]# vim index.html
- [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
- [root@localhost default]# /usr/local/nginx/sbin/nginx -s reload ## 改配置文件后重新加载
- [root@localhost vhost]# curl localhost
- This is the default site.
- (如果有错, 请查看 nginx.conaaa.com.conf 等配置是否有误)
指定默认虚拟主机:
1,vhost aaa 或者 0 等顺序
2,conf 里指定 default_server
12.8 Nginx 用户认证
[root@localhost vhost]# vim test.com.conf
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
. 如果没有安装 Apache, 那么可以 yum 安装
[root@localhost vhost]# yum install -y httpd
. 如果安装了 Apache, 那么可以直接使用 htpasswd
- [root@localhost vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd alex
- New password:
- Re-type new password:
- Adding password for user alex
- [root@localhost vhost]# htpasswd /usr/local/nginx/conf/htpasswd arron
- New password:
- Re-type new password:
- Adding password for user arro
- [root@localhost vhost]# !cat
- cat /usr/local/nginx/conf/htpasswd
- alex:$apr1$MLMPfmsl$oH/QYxybIFQSNj4xLCh4S/
- arron:$apr1$RfKhGXgJ$46dujc2WWwJDfWhhPn0311
- (再创建第二个用户则不用 -c )
- [root@localhost vhost]# curl -x127.0.0.1:80 test.com -I
- HTTP/1.1 401 Unauthorized
- Server: nginx/1.12.2
- Date: Tue, 13 Mar 2018 15:25:28 GMT
- Content-Type: text/html
- Content-Length: 195
- Connection: keep-alive
- WWW-Authenticate: Basic realm="Auth"
- [root@localhost vhost]# curl -u alex:123456 -x127.0.0.1:80 test.com
- <html> ## 由于去访问的是 index.html, 而还未创建, 所以 404
- <head><title>404 Not Found</title></head>
- <body bgcolor="white">
- <center><h1>404 Not Found</h1></center>
- <hr><center>nginx/1.12.2</center>
- </body>
- </html>
. 创建 test.com 主目录, 并编辑 index.html 文件
- [root@localhost vhost]# mkdir /data/wwwroot/test.com
- [root@localhost vhost]# echo "test.com"> /data/wwwroot/test.com/index.html
- [root@localhost vhost]# curl -u alex:123456 -x127.0.0.1:80 test.com
- test.com
. 如果需求为访问某个目录才需要认证, 那么可以改配置文件
- [root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
- [root@localhost vhost]# curl -x127.0.0.1:80 test.com
- test.com
- [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.12.2</center>
- </body>
- </html>
- ## 此时访问 test.com 是不需要认证, 而在访问 admin 目录是则被限制
- [root@localhost vhost]# vim /data/wwwroot/test.com/admin/
- [root@localhost vhost]# mkdir !$
- mkdir /data/wwwroot/test.com/admin/
- [root@localhost vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
- [root@localhost vhost]# curl -ualex:123456 -x127.0.0.1:80 test.com/admin/
- test.com admin dir
. 针对 url 限制, 例如 admin.php, 那么可以配置文件定义匹配:
- [root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
- [root@localhost vhost]# curl -x127.0.0.1:80 test.com/admin/
- test.com admin dir
- [root@localhost vhost]# curl -x127.0.0.1:80 test.com/admin.php
- <html>
- <head><title>401 Authorization Required</title></head>
- <body bgcolor="white">
- <center><h1>401 Authorization Required</h1></center>
- <hr><center>nginx/1.12.2</center>
- </body>
- </html>
- (匹配 admin.php 后,/admin / 不做限制, 只针对 admin.php 进行限制)
- .
创建 admin.php 才能访问到, 不创建则会有 404 错误
[root@localhost vhost]# vim /data/wwwroot/test.com/admin.php
- [root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
- [root@localhost vhost]# curl -ualex:123456 -x127.0.0.1:80 test.com/admin.php
- test test test
- (如果不是 test.com, 那么重定向到 test.com 下, permanent 是 301)
- [root@localhost vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
- [root@localhost vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html -I
- HTTP/1.1 404 Not Found
- Server: nginx/1.12.2
- Date: Tue, 13 Mar 2018 16:02:00 GMT
- Content-Type: text/html
- Content-Length: 169
- Connection: keep-alive
- [root@localhost vhost]# ls
- aaa.com.conf test.com.conf
- (由于没有定义 test3.com, 此时他会去访问默认虚拟主机, 第一个也就是 aaa.com)
来源: http://www.bubuko.com/infodetail-2526673.html