1. 源码安装nginx,并提供服务脚本。
1. 安装 Nginx 所需的 pcre 库 pcre 的全称为 perl compatible regular expressions ,中文译为 “ perl 兼容正则表达式 ” ,官方站点为 h ttp://www.pcre.org/ , 安装 pere 库是为了使 Nginx 支持具备 URI 重写功能的 rewrite 模块,如果不安装 pere 库,则 Nginx 无法使用 rewrite 模块功能, Nginx 的 rewrite 模块功能几乎是企业应用必须的。 2. 安装 Nginx Nginx 的安装非常简单,具体的操作过程及屏幕输岀如下。 ( 1 ) 检查并安装 Nginx 基础依赖包 pcre-devel 、 openssl-devel ( 2 ) 安装 openssl-devel Nginx 在使用 HTTPS 服务的时候要用到此模块,如果不安装 openssl 相关包,安装 Nginx 的过程中会报 错。 ( 3 ) 开始安装 Nginx( 4) 为 nginx 提供 SysV init 脚本 yum install -y pcre-devel # rpm -qa | egrep 'pcre-devel | openssl-devel' yum install -y openssl-devel # 创建用户和组 useradd nginx -s /sbin/nologin -M ./configure --user = nginx group = nginx --prefix = /usr/local/nginx --with-http_stub_status_module --with-http_ssl_module # 编译并安装 make && make install # vim /usr/lib/systemd/system/nginx.service [Unit] Description = nginx - high performance web server 4.3.1.4 nginx 深入剖析 1 、 Nginx 软件功能模块说明 ( 1 ) Nginx 核心功能模块 ( Core functionality ) Nginx 核心功能模块负责 Nginx 的全局应用,主要对应主配置文件的 Main 区块和 Events 区块区域,这 里有很多 Nginx 必须的全局参数配置。有关核心功能模
2. 配置基于域名的虚拟主机。
配置基于域名的虚拟主机
网页内容所在目录:/usr/share/nginx/html/
配置文件:/etc/nginx/conf.d/
主配置文件:/etc/nginx/nginx.conf
①创建网页内容所在目录并定义网页内容
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test{1,2}
[root@localhost html]# ls
50x.html index.html test1 test2
3. 配置nginx基于用户和地址的访问控制。
4. 配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义。
配置基于域名的虚拟主机
网页内容所在目录:/usr/share/nginx/html/
配置文件:/etc/nginx/conf.d/
主配置文件:/etc/nginx/nginx.conf
①创建网页内容所在目录并定义网页内容
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test{1,2}
[root@localhost html]# ls
50x.html index.html test1 test2
[root@localhost html]# echo test1 > test1/index.html
[root@localhost html]# echo test2 > test2/index.html
②编辑配置文件
[root@localhost html]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# ls
default.conf
[root@localhost conf.d]# mv default.conf{,.bak}
[root@localhost conf.d]# vim vhost.conf
添加以下内容:
server {
listen 80;
server_name www.test1.com;
location / {
root /usr/share/nginx/html/test1;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.test2.com;
location / {
root /usr/share/nginx/html/test2;
index index.html index.htm;
}
}
③重启服务
检查语法是否正确
[root@localhost conf.d]# nginx -t
[root@localhost conf.d]# systemctl restart nginx
④测试
在hosts文件中添加域名
[root@localhost nginx-1.18.0]# vim /etc/hosts
192.168.150.11 www.test1.com www.test2.com
访问测试
Windows访问nginx网页需要配置主机的hosts文件C:WindowsSystem32driversetchosts
配置nginx基于用户和地址的访问控制
基于用户的访问控制
①创建账号密码, 此账号密码就是用户访问网站时需要输入的。
安装httpd-tools安装包用于加密使用
[root@localhost ~]# yum install httpd-tools
[root@localhost ~]# htpasswd -c /opt/pwd tom
New password:
Re-type new password:
Adding password for user tom
②修改配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.test1.com;
location / {
root /usr/share/nginx/html/test1/;
index index.html index.htm;
auth_basic "Restricted";
auth_basic_user_file /opt/pwd;
}
}
③重启服务
[root@localhost ~]# systemctl restart nginx
④测试
配置nginx rewrite,要求如果访问不存在的任意网页都重定向到错误页面,错误页面内容自行定义
①定义错误页面内容:
[root@localhost ~]# echo "the require failed" > /usr/share/nginx/html/test1/err.html
②编辑配置文件:
[root@localhost ~]# vim /etc/nginx/conf.d/vhost.conf
修改内容如下:
server {
listen 80;
server_name www.test1.com;
location / {
root /usr/share/nginx/html/test1/;
index index.html index.htm;
if (!-f $request_filename) {
rewrite /.* err.html permanent;}
}
}
③重启服务
[root@localhost ~]# systemctl restart nginx
4 测试



