智传网优云课堂,专注分享IT技术
与IT技术爱好者一起共同学习进步

nginx配置文件详解及案例演示

以下教程是以CentOS 6.10和CentOS 7.6为演示案例

1. 安装EPEL仓库

EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL、CentOS和Scientific Linux.

CentOS7系统

[root@zcwyou ~]# rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm

CentOS6系统

[root@zcwyou ~]# rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm

或者无论是CentOS哪个版本,安装统一官方的EPEL源:

[root@zcwyou ~]# yum -y install epel-release

看到以下提示即可证明EPEL安装成功。

安装epel

2. 安装nginx

[root@zcwyou ~]# yum -y install nginx

安装成功的截图:
安装nginx

CentOS 6设置开机自动启动nginx

[root@zcwyou ~]# chkconfig nginx on

CentOS 7设置开机自动启动nginx

[root@zcwyou ~]# systemctl enable nginx

3. 配置nginx

3.1 创建一个站点的根目录

我打算使用nginx做文件共享服务

[root@zcwyou ~]# mkdir /usr/share/nginx/files

3.2 创建测试文件

[root@zcwyou ~]# touch /usr/share/nginx/files/test.txt

3.3 删除默认配置(其实是重命名),只适用于CentOS6,CentOS7不需要理会这步,因为它默认没有这个模版文件。

[root@zcwyou ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak

3.4 创建一个虚拟主机,用于文件共享

[root@zcwyou ~]# vi /etc/nginx/conf.d/file_server.conf

确保有以下内容:

server {
listen 80;
server_name files.example.com;
charset utf-8;
root /usr/share/nginx/files;
location / {
​ autoindex on;
​ autoindex_exact_size on;
​ autoindex_localtime on;
​ }
}

配置nginx虚拟主机

3.5 防火墙开放端口或服务

https采用TCP443端口,随后会有配置指引,强烈建议加密,即使加密对传输速度有影响

CentOS 7 firewalld:

[root@zcwyou ~]# firewall-cmd --add-service=http --permanent
[root@zcwyou ~]# firewall-cmd --add-service=https --permanent
[root@zcwyou ~]# firewall-cmd --reload

配置CentOS7防火墙

CentOS 6 iptables:

[root@zcwyou ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@zcwyou ~]# iptables -A INPUT -p tcp --dport 443 -j ACCEPT
[root@zcwyou ~]# service iptables save
[root@zcwyou ~]# service iptables reload

配置CentOS6防火墙

3.6 关闭SElinux

本教程忽略SElinux的设置

临时关闭SElinux,马上生效:

[root@zcwyou ~]# setenforce 0

永久关闭SElinux,把enforcing改为disabled

[root@zcwyou ~]# vi /etc/selinux/config 

永久关闭SElinux

CentOS7可以不关闭,重打SElinux标签即可。

[root@zcwyou ~]# restorecon -RvF /usr/share/nginx/

重打SElinux标签

3.7 启动nginx服务

先测试一下看看有没有报错

[root@zcwyou ~]# nginx -t

下图表示nginx配置测试通过,配置文件没有错误。

测试nginx

CentOS6启动nginx:

[root@zcwyou ~]# service nginx start

CentOS7启动nginx:

[root@zcwyou ~]# systemctl start nginx.service

3.8 在其它电脑上浏览器测试

假设你服务器的IP为192.168.1.100,你个人电脑的IP随意,可以访问服务器即可。 使用域名访问,需要在测试电脑上修改hosts文件,Windows的hosts文件位置C:\Windows\System32\drivers\etc\hosts
用记事本打开增加以下内容,如果权限问题,把文件复制到桌面修改完后再放回原位

192.168.1.100 files.example.com

IP地址跟你目前使用的Linux IP必须一致,我这里只是假设为192.168.1.100
Linux上查看IP地址的命令ip addr

修改Windows hosts文件

在浏览器上打开
http://files.example.com
应该可以看到test.txt这个文件

使用浏览器测试files站点

现在就可以通过浏览器共享文件了。

以后只需要把分享的文件放在目录/usr/share/nginx/files/里即可通过网页分享出去,注意一下权限,建议使用644,即使用命令chmod 644 /usr/share/nginx/files/*设置权限许可。

3.9 设置网站访问密码

搭建文件服务器有时候不想让别人任意访问,可以用到nginx自带的认证模块。 使用这两个字段
auth_basic表示的输入密码时的提示语
auth_basic_user_file则显示认证时的用户密码文件存放路径
留意以下auth_basic两行

[root@zcwyou ~]# vi /etc/nginx/conf.d/file_server.conf

server {
​ client_max_body_size 4G;
​ listen 80;
​ server_name files.example.com;
​ root /usr/share/nginx/files;
​ location / {
​ auth_basic “Restricted”;
​ auth_basic_user_file /etc/nginx/pass_file;
​ autoindex on;
​ autoindex_exact_size on;
​ autoindex_localtime on;
​ }
}

3.9.1 生成用户密码

安装htpasswd

[root@zcwyou ~]#  yum -y install httpd-tools

如果提示没有htpasswd这个命令,安装这个httpd-tools包

假设用户名为user1

[root@zcwyou ~]# htpasswd -c -d /etc/nginx/pass_file user1

按指引设置密码

先测试一下,看看有没有报错

[root@zcwyou ~]# nginx -t

3.9.2 重载配置

[root@zcwyou ~]# nginx -s reload

如果使用reload不生效就要使用restart

CentOS6:

[root@zcwyou ~]# service nginx restart

CentOS7:

[root@zcwyou ~]# systemctl restart nginx.service

3.9.3 测试

用浏览器测试一下,看看有没有密码要求

关闭浏览器再重新打开网页http://files.example.com

可以看到下图这种界面,输入用户名和密码即可浏览和下载里面的文件。

使用浏览器测试认证

4. 再创建一个网站

使用
/usr/share/nginx/html/index.html
作示范,你也可以自行修改页面内容

[root@zcwyou ~]# vi /etc/nginx/conf.d/www.conf

配置文件确保有以下内容:

#The www server
server {
​ listen 80;
​ listen [::]:80;
​ server_name www.example.com;
​ root /usr/share/nginx/html;
}

先测试一下配置,看看有没有报错

[root@zcwyou ~]# nginx -t

4.1 重载配置

[root@zcwyou ~]# nginx -s reload

如果使用reload不生效就要使用restart

CentOS6:

[root@zcwyou ~]# service nginx restart

CentOS7:

[root@zcwyou ~]# systemctl restart nginx.service

用记事本打开增加以下内容,如果权限问题,把文件复制到桌面修改完后再放回原位

192.168.1.100 www.example.com

IP地址跟你目前使用的Linux IP必须一致,我这里只是假设为192.168.1.100
Linux上查看IP地址的命令ip addr

hosts文件配置www

4.2 测试

在浏览器上分别打开
http://www.example.com
http://files.example.com

测试www主机页面

如果打不开,试试把浏览器关闭再重新打开。
如果还是打不开,试试把防火墙和SElinux关闭

CentOS6,关闭防火墙:

[root@zcwyou ~]# service iptables stop

CentOS7,关闭防火墙:

[root@zcwyou ~]# systemctl stop firewalld

CentOS 6或者CentOS 7临时关闭SElinux:

[root@zcwyou ~]# setenforce 0

5. 虚拟主机配置文件*.conf的其它参数

#指定进行用户
#user nobody;
#指定进程数
worker_processes 1;
#指定日志种类
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#限制连接数
events {
 worker_connections 1024;
}

http {
  #限制客户端上传文件大小
  client_max_body_size 20m;
  #访问记录
  access_log logs/access.log main;

#允许向服务器发送文件
  sendfile on;
}

5.1 网页中的图片处理

假设 /usr/share/nginx/www/images这个目录专门放图片,用于嵌入到网页里,
比如访问http://blog.example.com/images/1.jpg,就可以看到一张图片,但不允许客户端一次性查看images目录里所有文件,即不允许访问http://blog.example.com/images
配置如下,添加return 404;及前后两行共三行

[root@zcwyou ~]# vi /etc/nginx/conf.d/blog.conf

确保有以下文件

server {
 listen 80;
 listen [::]:80;
 server_name bolg.example.com;
 root /usr/share/nginx/blog;
 location = /images/ {
 return 404;
 }
}

创建工作目录,用于存放图片:

[root@zcwyou ~]# mkdir -p /usr/share/nginx/blog/images/

下载一张图片放在共享目录里:

[root@zcwyou ~]# yum -y install wget && wget -o /usr/share/nginx/blog/images/brand.png http://images2.linuxrumen.com/logo/brand.png

用记事本打开增加以下内容,如果权限问题,把文件复制到桌面修改完后再放回原位

192.168.1.100 blog.example.com

IP地址跟你目前使用的Linux IP必须一致,我这里只是假设为192.168.1.100
Linux上查看IP地址的命令ip addr

5.2 nginx.conf配置文件详解

参考https://www.zybuluo.com/phper/note/89391

6. 为nginx开启HTTPS并使用自我签署证书

6.1 创建自签名证书

只用于测试环境,线上网站请申请第三方认证机构颁发的证书
创建工作目录,执行以下指令

[root@zcwyou ~]# mkdir ~/tls && cd ~/tls

生成私钥server.key,运行:

[root@zcwyou ~]# openssl genrsa -des3 -out server.key 2048

按提示设置密码.以后使用此文件(通过openssl提供的命令或API)可能经常要求输入密码验证身份,如果想去除密码可以使用以下命令重新生成证书,但必须保证你这个私钥文件不会泄露

[root@zcwyou ~]# openssl rsa -in server.key -out server.key

输入刚刚的设置的密码

创建服务器证书的申请文件xxx.csr,运行以下指令,假设你已经搭建好三个虚拟主机, files_server、www、blog

生成主机files_server的申请文件files_server.csr

[root@zcwyou ~]# openssl req -new -key server.key -out files_server.csr

其中Country Name填CN,CommonName填主机名.例如你的URL为https://www.ABC.XYZ这里就可以填www).

生成主机www的申请文件www.csr

[root@zcwyou ~]# openssl req -new -key server.key -out www.csr

生成主机blog的申请文件blog.csr

[root@zcwyou ~]# openssl req -new -key server.key -out blog.csr

创建CA证书,即自身也是CA机构

[root@zcwyou ~]# openssl req -new -x509 -key server.key -out ca.crt -days 3650

创建自当前日期起有效期为期十年的服务器证书:
生成虚拟主机files_server的证书files_server.crt

[root@zcwyou ~]# openssl x509 -req -days 3650 -in files_server.csr -CA ca.crt -CAkey server.key -CAcreateserial -out files_server.crt

生成虚拟主机www的证书 www.crt

[root@zcwyou ~]# openssl x509 -req -days 3650 -in www.csr -CA ca.crt -CAkey server.key -CAcreateserial -out www.crt

生成虚拟主机blog的证书 blog.crt

[root@zcwyou ~]# openssl x509 -req -days 3650 -in blog.csr -CA ca.crt -CAkey server.key -CAcreateserial -out blog.crt

检查现在的文件

[root@zcwyou ~]# ll

可以看到一共生成了10个文件,其中*.crt和server.key就是你的nginx需要的证书文件.

复制相关文件到相应的目录

[root@zcwyou ~]# cp *.key /etc/pki/tls/private/
[root@zcwyou ~]# cp *.crt /etc/pki/tls/certs/

6.2 配置nginx https服务,留意.crt证书和密钥文件.key的位置

[root@zcwyou ~]# vi /etc/nginx/conf.d/ssl.conf

配置文件内容如下:

#HTTPS server configuration
#

#server {
#listen 443 ssl http2 default_server;
#listen [::]:443 ssl;
#server_name _;
#root /usr/share/nginx/html;
#
#ssl_certificate cert.pem;
#ssl_certificate_key cert.key;
#ssl_session_cache shared:SSL:1m;
#ssl_session_timeout 10m;
#ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_prefer_server_ciphers on;
#
##Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
#
#location / {
#}
#
#error_page 404 /404.html;
#location = /40x.html {
#}
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
#}
#}
#以下是文件服务主机的配置,autoindex开头的三行用于分享文件和目录
server {
​ listen 443 ssl;
​ listen [::]:443 ssl;
​ server_name files.example.com;
​ root /usr/share/nginx/files;
​ location / {
​ #auth_basic “Restricted”; #网页认证用
​ #auth_basic_user_file /etc/nginx/pass_file;#网页认证用
​ autoindex on;
​ autoindex_exact_size on;
​ autoindex_localtime on;
​ }
#
​ ssl_certificate /etc/pki/tls/certs/files_server.crt;
​ ssl_certificate_key /etc/pki/tls/private/server.key;
}
#以下是www主机的配置
server {
​ listen 443 ssl;
​ listen [::]:443 ssl;
​ server_name www.example.com;
#location / {
#root /usr/share/nginx/www;
#index index.html ;
#}
#
​ ssl_certificate /etc/pki/tls/certs/www.crt;
​ ssl_certificate_key /etc/pki/tls/private/server.key;
}
#以下是blog主机的配置
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name blog.example.com;
root /usr/share/nginx/blog;
#
​ ssl_certificate /etc/pki/tls/certs/blog.crt;
​ ssl_certificate_key /etc/pki/tls/private/server.key;
}

先测试一下,看看有没有报错

[root@zcwyou ~]# nginx -t

6.3 重载配置

[root@zcwyou ~]# nginx -s reload

如果使用reload不生效就要使用restart

CentOS6重启nginx:

[root@zcwyou ~]# service nginx restart

CentOS7重启nginx:

[root@zcwyou ~]# systemctl restart nginx.service

确保浏览器所在的系统(比如你的Windows系统)hosts文件有以下内容
如果权限问题,把文件复制到桌面修改完后再放回原位

192.168.1.100 www.example.com
192.168.1.100 files.example.com
192.168.1.100 blog.example.com

IP地址跟你目前使用的Linux IP必须一致,我这里只是假设为192.168.1.100
Linux上查看IP地址的命令ip addr

6.4 用浏览器测试三个网页:

https://files.example.com
https://www.example.com
https://blog.example.com

6.5 利用HTTP状态码301强制跳转到HTTPS

意思是当用户访问http://www.example.com时,自动跳转到https://www.example.com
如果是网页服务器,在虚拟主机上添加一行配置return 301 https://server_namerequest_uri;,示例:

[root@zcwyou ~]# vi /etc/nginx/conf.d/www.conf

确保有以下内容

server {
​ listen 80;
​ listen [::]:80;
​ server_name www.example.com;
​ return 301 https://server_namerequest_uri;
​ root /usr/share/nginx/html;
}

如果是文件共享服务器,在虚拟主机上添加一行配置return 301 https://server_namerequest_uri; 示例如下

[root@zcwyou ~]# vi /etc/nginx/conf.d/file_server.conf

location / {
​ autoindex on;
​ autoindex_exact_size on;
​ autoindex_localtime on;
​ return 301 https://server_namerequest_uri;
​ }

先测试一下,看看有没有报错

[root@zcwyou ~]# nginx -t

6.6 重载nginx配置

[root@zcwyou ~]# nginx -s reload

如果使用reload不生效就要使用restart

CentOS6重启nginx:

[root@zcwyou ~]# service nginx restart

CentOS7重启nginx:

[root@zcwyou ~]# systemctl restart nginx.service

6.7 用浏览器测试三个网页,看看能不能自动跳转到https

http://files.example.com
http://www.example.com
http://blog.example.com

赞(1)
未经允许不得转载:Linux入门学习到精通 » nginx配置文件详解及案例演示
分享到: 更多 (0)

学习QQ群:557371664

关注微信公众号自助视频学习

评论 抢沙发

评论前必须登录!