现需要通过域名跳转指定路径
浏览器访问https://nginxtest.com 出现重定向的次数过多问题
需要实现 访问 https://nginxtest.com 跳转至 https://nginxtest.com/#/login 路径
直接访问:https://nginxtest.com/#/login 可正常
在nginx配置文件中加入代理,代码如下:
server {
listen 80;
server_name https://nginxtest.com;
location / {
proxy_pass https://nginxtest.com/#/login;
}
}
当你访问https://nginxtest.com
的时候,他会直接访问https://nginxtest.com/#/login
题主,参考下这个例子:
检查你的nginx配置文件是否将80端口跟443端口写在了一个server里面。
配置文件头部添加
server {
listen 80;
index index.php index.htm index.html;
root /home;
return 301 https://www.xxx.com$request_uri;
}
监听80端口使用301重定向到https安全访问,不要用rewrite重写
location / {
proxy_pass https://123123/asd(这里填你的);
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
只配了这一段,访问一个域名下的/就会跳转到http://123123/asd;
但是在后台代码里写了一个访问任意资源时如果未登陆,先重定向到/asd/login进行登陆
这就会产生一个死循环,前台访问域名进入后台代码,后台重定向到域名/asd/login,前台检测到访问域名了就把/asd/login转成/asdasd/login了,因为访问了后台没有登陆,然后后台又会重定向…如此就会产生一个死循环,解决办法:
再做一层映射,把域名/asd访问直接转到后台代码的/asd路径下
location /asd {
proxy_pass https://123123/asd(同上);
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
具体代码如下:
location / {
proxy_pass https://nginxtest.com ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /# {
proxy_pass https://nginxtest.com ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
server
{
#这是要监听的端口
listen 80;
#这是监听的服务器地址,也代表要访问的地址
server_name www.xlcode.top;
#网站的启动页
index index.php index.html index.htm default.php default.htm default.html;
#网站所在的位置,这里是webapss
root /www/server/tomcat/webapps/;
#代表访问:www.xlcode.top/
location /
{
#访问www.xlcode.top,就会被aginx代理到http://www.xlcode.top:8080,这就是代理
proxy_pass "http://www.xlcode.top:8080";
proxy_set_header Host www.xlcode.top;
proxy_set_header X-Forwarded-For $remote_addr;
}
# 代表访问:www.xlcode.top/public/
location /public/
{ #nginx会代理到http://www.xlcode.top:8081
proxy_pass "http://www.xlcode.top:8081";
}
# 所有的静态文件(gif,jpg)都会被映射到这个root根目录,所以当您访问另外一个文件夹下的静态资源时,会报404
location ~*.*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
expires 12h;
}
#代表访问:www.xlcode.top/news
location /news
{ #nginx会代理到目录 /www/springboot/news;
root /www/springboot;
}
}
server
{
#这是要监听的端口8081
listen 8081;
server_name www.xlcode.top;
root /www/public/;
# 访问www.xlcode.top:8081会映射到 /www/public/目录下
location /
{
root /www/public;
}
# 访问www.xlcode.top:8081/public会映射到 /www/public/目录下,要仔细比较这二者的区别
location /public
{
root /www;
# index index.html;
}
}
经过我的测试如下nginx配置可以满足,访问 https://nginxtest.com/ 跳转至 https://nginxtest.com/#/login 路径,直接访问:https://nginxtest.com/#/login 正常
server {
listen 80;
server_name nginxtest.com;
if ( $host != 'www.nginxtest.com' ) {
rewrite ^/(.*) https://nginxtest.com/#/login;
}
location / {
root /data/www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}