为什么nginx会与错误301 HTTP1.1/Moved一起工作?
我正在尝试在我的nginxweb服务器(Ubuntu)上配置Location指令 我可以访问: 但是,当我试图最终获得不带斜杠的访问权限时,就像: 我得到一个错误,永久移动了301 HTTP1.1 我有以下nginx配置:为什么nginx会与错误301 HTTP1.1/Moved一起工作?,nginx,url,webserver,Nginx,Url,Webserver,我正在尝试在我的nginxweb服务器(Ubuntu)上配置Location指令 我可以访问: 但是,当我试图最终获得不带斜杠的访问权限时,就像: 我得到一个错误,永久移动了301 HTTP1.1 我有以下nginx配置: user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
server {
listen 80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /app1/ {
root /var/www/html/;
index index.html;
try_files $uri $uri/ /app1/index.html;
}
}
看起来一切都很好
和以下default.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
server {
listen 80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /app1/ {
root /var/www/html/;
index index.html;
try_files $uri $uri/ /app1/index.html;
}
}
卷曲输出
root@ubuntu-测试:/etc/nginx/sites available#curl 127.0.0.1/app1/-Iv
*正在尝试127.0.0.1。。。
*TCP_节点集
*连接到127.0.0.1(127.0.0.1)端口80(#0)
>HEAD/app1/HTTP/1.1
>主持人:127.0.0.1
>用户代理:curl/7.58.0
>接受:*/*
>
root@ubuntu-测试:/etc/nginx/sites available#curl 127.0.0.1/app1-Iv
*正在尝试127.0.0.1。。。
*TCP_节点集
*连接到127.0.0.1(127.0.0.1)端口80(#0)
>HEAD/app1 HTTP/1.1
>主持人:127.0.0.1
>用户代理:curl/7.58.0
>接受:*/*
>
为什么会发生这种情况?Nginx选择
位置/
块来处理URI/app1
,因为没有其他位置更适合。看
try\u files
语句的$uri/
术语通知Nginx将/
附加到与目录匹配的任何uri。目录/var/www/html/app1
符合此要求,因此生成301重定向以将/
附加到URI。有关详细信息,请参阅
此外,以/
结尾并指向目录的URI的默认行为是在该目录中搜索与index
指令匹配的文件。有关详细信息,请参阅
可能会偏离此默认行为,但您需要对配置进行一些更改。如果要使
位置/app1/
与/app1
匹配,则需要丢失尾随的/
。如果要避免301重定向,您的try\u文件
指令需要丢失$uri/
术语。您还将丢失默认的索引处理,因此索引
指令将无效。我试图删除try\u文件
指令,并且我已从/app1
位置删除了尾随斜杠。但它仍然不起作用。http:127.0.0.1/app1响应为301错误。如果删除try\u files
指令,Nginx将恢复为默认行为,即生成301重定向。您需要保留try\u files
指令,只删除$uri/
术语。