2-nginx-静态-防盗-跨域

小案例

需求如下

(1)有如下访问:
    http://192.168.47.8:8081/server1/location1
        访问的是:index_sr1_location1.html
    http://192.168.47.8:8081/server1/location2
        访问的是:index_sr1_location2.html
    http://192.168.47.8:8082/server2/location1
        访问的是:index_sr2_location1.html
    http://192.168.47.8:8082/server2/location2
        访问的是:index_sr2_location2.html
(2)如果访问的资源不存在,
    返回自定义的404页面
(3)将/server1和/server2的配置使用不同的配置文件分割
    将文件放到/home/www/conf.d目录下,然后使用include进行合并
(4)为/server1和/server2各自创建一个访问日志文件

把原有的nginx.conf 配置文件备份

[root@Nginx-1 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@Nginx-1 conf]# pwd
/usr/local/nginx/conf
[root@Nginx-1 conf]# cp nginx.conf.default nginx.conf #通过模板生成新的配置文件

创建如下目录

[root@Nginx-1 sbin]# tree /home/www/
/home/www/
├── conf.d # 创建
│?? ├── server1.conf # 创建
│?? └── server2.conf# 创建
└── myweb# 创建
    ├── 404.html# 创建
    ├── server1# 创建
    │?? ├── location1# 创建
    │?? │?? └── index_sr1_location1.html# 创建
    │?? ├── location2# 创建
    │?? │?? └── index_sr1_location2.html# 创建
    │?? └── logs# 创建
    │??     └── access.log# 创建
    └── server2# 创建
        ├── location1# 创建
        │?? └── index_sr2_location1.html# 创建
        ├── location2# 创建
        │?? └── index_sr2_location2.html# 创建
        └── logs# 创建
            └── access.log# 创建

10 directories, 9 files
[root@Nginx-1 sbin]# 

配置主配置文件

/usr/local/nginx/conf/nginx.conf


##全局块 begin##
#配置允许运行Nginx工作进程的用户和用户组 www
user www;

#配置运行Nginx进程生成的worker进程数2
worker_processes 2;

#配置Nginx服务器运行对错误日志存放的路径  错误日志会存放在安装目录下的logs/error.log
error_log logs/error.log; //
#配置Nginx服务器允许时记录Nginx的master进程的PID文件路径和名称
pid logs/nginx.pid;

#配置Nginx服务是否以守护进程方法启动 默认不配置也是on
#daemon on;  
##全局块 end##


##events块 begin##
events{
    #设置Nginx网络连接序列化
    accept_mutex on;

    #设置Nginx的worker进程是否可以同时接收多个请求
    multi_accept on;

    #设置Nginx的worker进程最大的连接数
    worker_connections 1024;

    #设置Nginx使用的事件驱动模型
    use epoll;
}
##events块 end##


##http块 start##
http{
    #定义MIME-Type
    include mime.types;
    default_type application/octet-stream;

    #配置允许使用sendfile方式运输 提升静态资源访问效率
    sendfile on; 

    #配置连接超时时间
    keepalive_timeout 65;

    #配置请求处理日志格式 
    log_format server1 '===>server1 access log';
    log_format server2 '===>server2 access log';

    ##server块 开始##
    include /home/www/conf.d/*.conf; #下面的配置文件在这个路径下
    ##server块 结束##
}
##http块 end##

分别配置两个nginx页面配置文件

vim /home/www/conf.d/server1.conf

server {
        listen 8081;
        servername localhost;
        access_log /home/www/myweb/server1/logs/access.log server1 ;
        location /server1/location1{
        root /home/www/myweb;
        index index_sr1_location1.html
        }
        error_page 404 /404.html;
        location = /404.html {
                root /home/www/myweb;
                index 404.html;
        }

}

或者配置成这样

server{
        #配置监听端口和主机名称
        listen 8081;
        server_name localhost;
        #配置请求处理日志存放路径
        access_log /home/www/myweb/server1/logs/access.log server1;
        #配置错误页面
        error_page 404 /404.html;
        #配置处理/server1/location1请求的location
        location /server1/location1{
            root /home/www/myweb;
            index index_sr1_location1.html;
        }
        #配置处理/server1/location2请求的location
        location /server1/location2{
            root /home/www/myweb;
            index index_sr1_location2.html;
        }
        #配置错误页面转向
        location = /404.html {
            root /home/www/myweb;
            index 404.html;
        }
}

vim /home/www/conf.d/server2.conf

server{
        #配置监听端口和主机名称
        listen 8082;
        server_name localhost;
        #配置请求处理日志存放路径
        access_log /home/www/myweb/server2/logs/access.log server2;
        #配置错误页面,对404.html做了定向配置
        error_page 404 /404.html;
        #配置处理/server1/location1请求的location
        location /server2/location1{
            root /home/www/myweb;
            index index_sr2_location1.html;
        }
        #配置处理/server2/location2请求的location
        location /server2/location2{
            root /home/www/myweb;
            index index_sr2_location2.html;
        }
        #配置错误页面转向
        location = /404.html {
            root /home/www/myweb;
            index 404.html;
        }
    }

写入网页内容

[root@Nginx-1 conf.d]# echo "index_sr2_location1.html server "> /home/www/myweb/server2/location1/index_sr2_location1.html
[root@Nginx-1 conf.d]# echo "index_sr2_location2.html server "> /home/www/myweb/server2/location2/index_sr2_location2.html

[root@Nginx-1 conf.d]# echo "index_sr1_location1.html server "> /home/www/myweb/server1/location1/index_sr1_location1.html
[root@Nginx-1 conf.d]# echo "index_sr1_location2.html server "> /home/www/myweb/server1/location2/index_sr1_location2.html
[root@Nginx-1 conf.d]# echo "404 error "> /home/www/myweb/404.html

测试nginx配置文件合规

[root@Nginx-1 sbin]# pwd
/usr/local/nginx/sbin
[root@Nginx-1 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@Nginx-1 sbin]# 

image-20220620144004972

image-20220620144028382

image-20220620144110543

image-20220620144135815

image-20220620144157240

image-20220620144216901

每访问一次 就有一条日志输出

image-20220620153738347

Nginx配置成系统服务

(1) 在/usr/lib/systemd/system目录下添加nginx.service,内容如下:

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=default.target

(2)添加完成后如果权限有问题需要进行权限设置

chmod 755 /usr/lib/systemd/system/nginx.service

(3)使用系统命令来操作Nginx服务

启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx

可以正常使用命令

[root@Nginx-1 ~]# systemctl start nginx
[root@Nginx-1 ~]# systemctl status nginx
● nginx.service - nginx web service
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2022-06-20 15:46:06 CST; 5s ago
     Docs: http://nginx.org/en/docs/
  Process: 1199 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1198 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 1201 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1201 nginx: master process /usr/local/nginx/sbin/nginx
           ├─1202 nginx: worker process
           └─1203 nginx: worker process

6月 20 15:46:06 Nginx-1 systemd[1]: Starting nginx web service...
6月 20 15:46:06 Nginx-1 nginx[1198]: nginx: the configuration file /...k
6月 20 15:46:06 Nginx-1 nginx[1198]: nginx: configuration file /usr/...l
6月 20 15:46:06 Nginx-1 systemd[1]: Failed to parse PID from file /u...t
6月 20 15:46:06 Nginx-1 systemd[1]: Started nginx web service.
Hint: Some lines were ellipsized, use -l to show in full.

Nginx命令配置到系统环境

[root@Nginx-1 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments:
[root@Nginx-1 ~]# nginx -v
-bash: nginx: 未找到命令
[root@Nginx-1 ~]# 

(1)修改/etc/profile文件

vim /etc/profile
在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin

(2)使之立即生效

source /etc/profile

(3)执行nginx命令

[root@Nginx-1 ~]# vim /etc/profile
[root@Nginx-1 ~]# source /etc/profile
[root@Nginx-1 ~]# nginx -v
nginx version: nginx/1.16.1
[root@Nginx-1 ~]# 

Nginx静态资源部署

Nginx静态资源概述

我们请求的内容就分为两种类型,一类是静态资源、一类是动态资源。 静态资源即指在服务器端真实存在并且能直接拿来展示的一些文件,比如常见的html页面、css文件、js文件、图 片、视频等资源; 动态资源即指在服务器端真实存在但是要想获取需要经过一定的业务逻辑处理根据不同的条件展示在页面不同 这 一部分内容,比如说报表数据展示、根据当前登录用户展示相关具体数据等资源;

Nginx处理静态资源的内容,我们需要考虑下面这几个问题:

(1)静态资源的配置指令 (2)静态资源的配置优化 (3)静态资源的压缩配置指令 (4)静态资源的缓存处理 (5)静态资源的访问控制,包括跨域问题和防盗链问题

Nginx静态资源的配置指令

一下指令 通过nginx访问静态资源的时候会用到,但不是nginx为配置静态资源的而设置的专用指令。

listen指令

listen:用来配置监听端口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机猿巧合.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值