我已经以这种方式配置了Nginx:
#/etc/nginx/conf.d/default.conf
server {
listen 8081 default_server;
listen [::]:8081 default_server ipv6only=on;
server_name localhost;
listen 443 ssl;
location /site/ {
proxy_pass http://127.0.0.1:8084/;
}
# 404 occured
#location /site/secure/ {
# proxy_redirect http://localhost:8081/site/secure/ https://localhost/site/secure/;
#}
location / {
root /srv;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
}
我启用了https。 我有几个网址:
http://127.0.0.1:8081/site/secure/
http://127.0.0.1:8081/sute/path1/...
http://127.0.0.1:8081/sute/path2/...
我想仅将http://127.0.0.1:8081/site/secure/*
次请求重定向到https://127.0.0.1/site/secure/*
,以便用户无法使用简单的http来使用此网址。
但是当我使用此重定向运行Nginx时:
location /site/secure/ {
proxy_redirect http://localhost:8081/site/secure/ https://localhost/site/secure/;
}
我收到404回复:
<html>
<head>
<title>404 Not Found</title>
</head>
<body bgcolor="white">
<center>
<h1>404 Not Found</h1>
</center>
<hr>
<center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
如何配置nginx以正确方式将某些请求重定向到https?
答案 0 :(得分:1)
proxy_redirect
是错误的指令。您需要使用return 301 https://...
之类的内容将客户端从http
移至https
。
最干净的方法是使用两个server
块,并使用include
指令引入通用配置语句。例如:
server {
listen 8081 default_server;
listen [::]:8081 default_server ipv6only=on;
include /path/to/common/bits;
location ^~ /site/secure/ {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
include /path/to/common/bits;
}