我有一个设置,其中haproxy在example.com:80上侦听,并将HTTP请求代理到在服务器20080上侦听的nginx实例。
nginx所做的只是从提供静态文件/usr/share/nginx/html
。
因此,例如,http://example.com/doc/
映射到/usr/share/nginx/html/doc/
。
但是,请求http://example.com/doc
(不带斜杠)将导致301重定向到http://example.com:20080/doc/
:
$ curl -i http://example.com/doc
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Tue, 09 Dec 2014 15:10:44 GMT
Content-Type: text/html
Content-Length: 185
Location: http://example.com:20080/doc/
请注意,nginx已在URL中包含端口20080。但是,这是不必要的,因为该站点的面向公众的URL是,http://example.com/
因此重定向应该http://example.com/doc/
改为。
向nginx解释这一点的最简单方法是什么?
/etc/nginx/nginx.conf
:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/default.conf
:
server {
listen 20080 default_server;
include /etc/nginx/default.d/*.conf;
root /usr/share/nginx/html;
index index.html;
location /doc/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd/doc;
}
}
该nginx.conf
文件是CentOS nginx软件包1.0.15版中的默认文件。这是一个古老的版本,但是我无法控制CentOS或它的软件包版本。
配置您的HAProxy,或使用port_in_redirect off;
指令。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句