侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Nginx(二):HTTP服务的相关配置

2023-11-13 星期一 / 0 评论 / 0 点赞 / 54 阅读 / 9502 字

Nginx(二):HTTP服务的相关配置   注意:以下实验域名解析通过客户端hosts文件实现; 虚拟服务器相关配置:   nginx支持基于端口和基于主机名的虚拟主机,不支持基于IP的虚拟主机;

Nginx(二):HTTP服务的相关配置

  注意:以下实验域名解析通过客户端hosts文件实现;

虚拟服务器相关配置:

  nginx支持基于端口和基于主机名的虚拟主机,不支持基于IP的虚拟主机;

基于端口:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    location / {        root /web/www;        index index.html index.htm;    }}server {    listen 8080;    server_name www.chencer.org;    location / {        root /web/port;        index index.html index.htm;    }}

提供网页文件:

[root@node0 ~]# echo "www.chencer.org:80" > /web/www/index.html[root@node0 ~]# echo "www.chencer.org:8080" > /web/port/index.html

重启服务后查看监听端口:

[root@node0 ~]# ss –tnl

访问测试:

基于主机名:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    location / {        root /web/www;        index index.html index.htm;    }}server {    listen 80;    server_name web.chencer.org;    location / {        root /web/web;        index index.html index.htm;    }}

提供网页文件:

[root@node0 ~]# echo "www.chencer.org" > /web/www/index.html [root@node0 ~]# echo "web.chencer.org" > /web/web/index.html

重启服务后,访问测试:

访问控制:

  nginx支持基于IP和基于用户的访问控制;

基于IP:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    location / {        root /web/www;        index index.html index.htm;        deny 192.168.1.2;        allow 192.168.1.0/24;        deny all;    }}

重启服务后,访问测试:

基于用户:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    location / {        root /web/www;        index index.html index.htm;         auth_basic "Admin Area";         auth_basic_user_file /etc/nginx/.htpasswd;    }}

认证文件由httpasswd命令创建:

[root@node0 ~]# yum install httpd-tools[root@node0 ~]# htpasswd -mc /etc/nginx/.htpasswd tomNew password: Re-type new password: Adding password for user tom

重启服务后,访问测试;

压缩功能:

  nginx将响应报文发送至客户端之前可以启用压缩功能,这能够有效地节约带宽,并提高响应至客户端的速度。通常编译nginx默认会附带gzip压缩的功能,因此,可以直接启用之;

提供一个较大的测试页:

[root@node0 ~]# cp /var/log/messages /web/www/index.html

未压缩访问测试:

启用压缩:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    gzip on;    gzip_http_version 1.0;    gzip_comp_level 2;    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;    gzip_disable msie6;    location / {        root /web/www;        index index.html index.htm;    }}

重启服务后,访问测试;

建立下载站点autoindex模块:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    location / {        root /web/www;        index index.html index.htm;    }    location /download {        root /web/www;        autoindex on;    }}[root@node0 ~]# mkdir /web/www/download[root@node0 ~]# cp /var/log/{cron,messages,maillog}  /web/www/download/

重启服务,访问测试;

防盗链:

定义合规的引用:

valid_referers none | blocked | server_names | string ...;none:没有referer    blocked:被清除    server_names:开放的主机名    string

拒绝不合规的引用:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    location / {        root /web/www;        index index.html index.htm;    }    location ~* /.(jpg|png|gif|jpeg)$ {        root /web/www;        valid_referers none blocked www.chencer.org;        if  ($invalid_referer) {            rewrite ^/.*$ http://www.chencer.org/403.html;        }    }}server {    listen 80;    server_name web.chencer.org;    location / {        root /web/web;        index index.html index.htm;    }}[root@node0 ~]# vim /web/www/index.htmlwww.chencer.org<img src="http://www.chencer.org/images/1.jpg">[root@node0 ~]# vim /web/web/index.htmlweb.chencer.org<img src="http://www.chencer.org/images/1.jpg">

重启服务后,访问测试:

URL rewrite,地址重写;

rewrite regex replacement [flag];flag:last:一旦被当前规则匹配并重写后立即停止检查后续的其它rewrite的规则,而后通过重写后的规则重新发起请求;break:一旦被当前规则匹配并重写后立即停止后续的其它rewrite的规则,而后继续由nginx进行后续操作;redirect:返回302临时重定向;permanent:返回301永久重定向;[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    location / {        root /web/www;        index index.html index.htm;    }    location /imgs {        root /web/www;        rewrite ^/imgs/(.*/.(jpg|png|gif|jpeg))$ /images/$1 last;    }}[root@node0 ~]# mkdir /web/www/imgs[root@node0 ~]# ls /web/www/imgs[root@node0 ~]# ls /web/www/images1.jpg

重启服务,访问测试:

开启服务器状态页:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen 80;    server_name www.chencer.org;    location / {        root /web/www;        index index.html index.htm;    }    location /server_status {        stub_status on;    }}

重启服务,查看状态页:

Https:

服务器自建CA,自签证书:

[root@node0 ~]# (umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)[root@node0 ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655[root@node0 ~]# touch /etc/pki/CA/{index.txt,serial}[root@node0 ~]# echo 01 > /etc/pki/CA/serial

创建证书,签署请求:

[root@node0 ~]# (umask 077;openssl genrsa -out /etc/nginx/nginx.key 2048)[root@node0 ~]# openssl req -new -key /etc/nginx/nginx.key -out /etc/nginx/nginx.csr[root@node0 ~]# openssl ca -in /etc/nginx/nginx.csr -out /etc/nginx/nginx.crt -days 3650

启用nginx_ssl功能:

[root@node0 ~]# vim /etc/nginx/nginx.confserver {    listen       443 ssl;    server_name  www.chencer.org;    ssl_certificate      nginx.crt;    ssl_certificate_key  nginx.key;    ssl_session_cache    shared:SSL:1m;    ssl_session_timeout  5m;    ssl_ciphers  HIGH:!aNULL:!MD5;    ssl_prefer_server_ciphers  on;    location / {        root   /web/www;        index  index.html index.htm;    }}

重启服务,查看端口:

[root@node0 ~]# service nginx restart[root@node0 ~]# ss –tnl

443端口处于监听状态;

客户端浏览器安装证书,并访问测试:

广告 广告

评论区