nginx的一些测试location-alias

nginx测试

[root@test ~]# yum -y install gcc make pcre-devel openssl-devel
[root@test ~]# tar xf nginx-1.17.6.tar.gz
[root@test ~]# cd nginx-1.17.6/
[root@test nginx-1.17.6]# ./configure  --prefix=/usr/local/nginx  --user=nginx  --with-http_ssl_module
[root@test nginx-1.17.6]#  make && make install
[root@test nginx-1.17.6]# useradd nginx -s /sbin/nologin

worcker数量调整

[root@test nginx]# head conf/nginx.conf
#user  nobody;
worker_processes  1;
[root@test nginx]# /usr/local/nginx/sbin/nginx
[root@test nginx]# ps -ef | grep nginx
nginx      5499   5498  0 15:43 ?        00:00:00 nginx: worker process
[root@test nginx]# head conf/nginx.conf
#user  nobody;
worker_processes  2;      //也可以是auto,一般设置成cpu核数-1
[root@test nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@test nginx]# ps -ef | grep nginx
nginx      5525   5498  0 15:46 ?        00:00:00 nginx: worker process
nginx      5526   5498  0 15:46 ?        00:00:00 nginx: worker process

events

events {
    worker_connections  1024;
}

:set paste 不会添加tab键,自动填充等,完整的粘过去

配置server监听80端口

[root@test ~]# vim /usr/local/nginx/conf/nginx.conf
 32     include /etc/nginx.d/*.conf;       //添加扩展配置
 33     #gzip  on;
 34
 35     server {
...
[root@test nginx]# mkdir -p /etc/nginx/
[root@test ~]# cat /etc/nginx.d/test.conf
    server {
        listen       80;
        server_name  localhost;
        root /opt/nginx;    //默认是 /usr/local/nginx/html

        location / {
        }
    }
[root@test nginx]# mkdir -p /opt/nginx/
[root@test ~]# cp /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@test 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@test ~]# nginx -s reload
[root@test ~]# curl 192.168.1.11:80
<head><title>403 Forbidden</title></head>
[root@test ~]# tail /usr/local/nginx/logs/access.log
192.168.1.11 - - [09/Apr/2022:16:22:36 +0800] "GET / HTTP/1.1" 403 153 "-" "curl/7.29.0"
[root@test ~]# tail /usr/local/nginx/logs/error.log
2022/04/09 16:22:36 [error] 5906#0: *9 directory index of "/opt/nginx/" is forbidden, client: 192.168.1.11, server: localhost, request: "GET / HTTP/1.1", host: "192.168.1.11:80"
[root@test ~]# echo "i am 80"> /opt/nginx/index.html
[root@test ~]# curl 192.168.1.11
i am 80

下载文件

[root@test ~]# mkdir -p /opt/nginx/html/
[root@test ~]# echo "url下载测试" >/opt/nginx/html/data.jsp
浏览器直接访问192.168.1.11:/html/data.jsp

打开文件目录功能下载

[root@test ~]# cat /etc/nginx.d/test.conf

    server {
        listen       80;
        server_name  localhost;
        root /opt/nginx;
//以下四行
        autoindex on;
        autoindex_exact_size on;
        autoindex_localtime on;
        charset utf-8,gbk;

    location / {
        }
    }
[root@test ~]# echo "下载测试" >/opt/nginx/data.txt
[root@test ~]# rm -rf /opt/nginx/index.html       //优先显示index文件
[root@test ~]# 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@test ~]# nginx -s reload
浏览器访问http://192.168.1.11
Index of /
../
html/                                              09-Apr-2022 16:38                   -
data.txt                                           09-Apr-2022 16:36                  13

单一监听端口 配置多servername 域名修改

[root@test ~]# cat /etc/nginx.d/test.conf
[root@test ~]# cat /etc/nginx.d/test.conf

    server {
        listen       80;
        server_name  local;
        root /opt/nginx;

    location / {
        }
    }
    server {
        listen       80;
        server_name  test;
        root /opt/nginx/html;

    location / {
        }
    }
[root@test ~]# echo "192.168.1.11   local test" >/etc/hosts
[root@test ~]# echo "servername test" >/opt/nginx/html/index.html
[root@test ~]# echo "servername locahost" >/opt/nginx/index.html
[root@test ~]# 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@test ~]# nginx -s reload
[root@test ~]# curl local
servername locahost
[root@test ~]# curl test
servername test
[root@test ~]# curl 192.168.1.11:8080       
servername locahost    显示的是第一个servername出现的配置

localtion路径测试

访问的路径是: location的路径 + 下方root的绝对路径

[root@test ~]# cat /etc/nginx.d/test.conf

    server {
        listen       8080;
        server_name  local;
        root /opt/nginx;

    location / {
        }

    location ~ ^/map1 {
          root /opt/nginx/html;    //绝对路径
        }
    }
[root@test ~]# nginx -s reload
[root@test ~]# rm -rf /opt/nginx/*
[root@test ~]# curl 192.168.1.11:8080
403 Forbidden
[root@test ~]# echo "/opt/nginx/index.html"  >/opt/nginx/index.html
[root@test ~]# curl 192.168.1.11:8080
/opt/nginx/index.html
[root@test ~]# curl 192.168.1.11:8080/map1
404 Not Found
[root@test ~]# tail -n1 /usr/local/nginx/logs/error.log
 open() "/opt/nginx/html/map1" failed (2: No such file or directory), client: 

通过看错误日志,可以确定实际访问的路径是:IP + 端口 + location的路径 + 下方root的绝对路径

[root@test ~]# mkdir -p /opt/nginx/html/map1
[root@test ~]# echo "/opt/nginx/html/map1" >/opt/nginx/html/map1/index.html
[root@test ~]# curl 192.168.1.11:8080/map1/index.html    
/opt/nginx/html/map1
[root@test ~]# curl 192.168.1.11:8080/map1/
/opt/nginx/html/map1
//访问必须完整带/,不然不能自动定位到map1路径下的index.html文件
[root@test ~]# curl 192.168.1.11:8080/map1
<head><title>301 Moved Permanently</title></head>

通过看错误日志,可以确定访问的路径是: location的路径 + 下方root的绝对路径

=  :精确匹配(必须全部相等)
 ~  :大小写敏感(正则表达式)
 ~* :忽略大小写(正则表达式),这里要注意忽略大小写的意思是请求的字符大小写都可以,
 但是不会进行大小转换,请求的大小写对应的文件必须存在。
 ^~ :只需匹配uri部分
 @  :内部服务跳转
[root@test ~]# cat /etc/nginx.d/test.conf
...

    location ~ ^/map1 {
          root html;    
//改为相对路径,默认的路径是/usr/local/nginx ,与server里的root无关。建议写绝对路径好区分。
        }
    }
[root@test ~]# nginx -s reload
[root@test ~]# curl 192.168.1.11:8080/map1/index.html
<head><title>404 Not Found</title></head>
[root@test ~]# tail -n1  /usr/local/nginx/logs/error.log
 [error] 9632#0: *62 open() "/usr/local/nginx/html/map1/index.html" failed (2: No such file or directory), client: 192.168.1.11, server: local, request: "GET /map1/index.html HTTP/1.1", host: "192.168.1.11:8080"

错误日志看到访问路径 **/usr/local/nginx/**html/map1/index.html ,这是默认的nginx路径。可以肯定,这里location加入后的访问路径,跟上面的root路径没什么关系。也就是说:server里的路径只决定了自己的根路径。

alias配置

实际访问的路径是: 下方的绝对路径

[root@test ~]# cat /etc/nginx.d/test.conf

    server {
        listen       8080;
        server_name  local;
        root /opt/nginx;

    location / {
        }

    location ~ ^/map1 {
          alias /opt/nginx/html;
        }
    }

[root@test ~]# curl 192.168.1.11:8080/map1/
<head><title>403 Forbidden</title></head>
[root@test ~]# tail -n1  /usr/local/nginx/logs/error.log
2022/04/09 19:12:40 [error] 9889#0: *76 directory index of "/opt/nginx/html" is forbidden, 

继续测试localtion

[root@test ~]# cat /etc/nginx.d/test.conf

    server {
        listen       8080;
        server_name  local;
        root /opt/nginx;

    location / {
        }

    location ~ ^/map1 {
          alias /opt/nginx/html;
        }
    }
[root@test ~]# curl 192.168.1.11:8080/map1/
/opt/nginx/html/map1
[root@test ~]# cat /etc/nginx.d/test.conf

    server {
        listen       8080;
        server_name  local;
        root /opt/nginx;

    location / {
        }

    location ~ ^/map1/(.*)$ {
          root /opt/nginx/html/$1;     //$1调用前面location的
        }
    }
[root@test ~]# nginx -s reload
[root@test ~]# curl 192.168.1.11:8080/map1
<head><title>404 Not Found</title></head>
[root@test ~]# tail -n1  /usr/local/nginx/logs/error.log
open() "/opt/nginx/map1" failed (2: No such file or directory)
[root@test ~]# curl 192.168.1.11:8080/map1/
<head><title>404 Not Found</title></head>
[root@test ~]# tail -n1  /usr/local/nginx/logs/error.log
[error] 9988#0: *88 open() "/opt/nginx/html/index.html/map1/index.html" failed 
[root@test ~]# curl 192.168.1.11:8080/map1/test
<head><title>404 Not Found</title></head>
[root@test ~]# tail -n1  /usr/local/nginx/logs/error.log
open() "/opt/nginx/html/test/map1/test
[root@test ~]# curl 192.168.1.11:8080/map1/test/
/opt/nginx/html/test//map1/test/index.html" is not found
[root@test ~]# cat /etc/nginx.d/test.conf

    server {
        listen       8080;
        server_name  local;
        root /opt/nginx;

    location / {
        }

    location  /map1 {
          root /opt/nginx/html;     //$1调用前面location的
        }
    }
[root@test ~]# curl 192.168.1.11:8080/map1/
/opt/nginx/html/map1
[root@test ~]# curl 192.168.1.11:8080/map1/test
 open() "/opt/nginx/html/map1/test
[root@test ~]# curl 192.168.1.11:8080/map1/test/
/opt/nginx/html/map1/test/index.html is not found
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值