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
403 Forbidden
[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
[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
301 Moved Permanently
通过看错误日志,可以确定访问的路径是: 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
404 Not Found
[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里的路径只决定了自己的根路径。
实际访问的路径是: 下方的绝对路径
[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/
403 Forbidden
[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
404 Not Found
[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/
404 Not Found
[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
404 Not Found
[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