I'm trying to redirect a location to another location, but for some reason it doesn't work. My nginx service is running under a Docker container on 8080:80
port.
我正在尝试将一个位置重定向到另一个位置,但由于某种原因不起作用。我的nginx服务在8080:80端口的Docker容器下运行。
I have /portal
that shows my web application correctly and I would like nginx redirects from /
to /portal
. I've tried some approaches using location /
or location = /
, but with no success.
我有/门户,可以正确显示我的Web应用程序,我希望nginx从/到/门户重定向。我尝试了一些使用Location/或Location=/的方法,但没有成功。
The /portal
can be accessed normally by
可以通过以下方式正常访问/门户
http://localhost:8080/portal
but for some reason when I access
但出于某种原因,当我访问
http://localhost:8080/
the browser redirects to
浏览器重定向到
https://localhost/portal
instead of redirecting to:
而不是重定向至:
http://localhost:8080/portal
Locations:
地点:
upstream webportal {
server portal_container:8080;
}
location / {
return 301 /portal;
}
location /portal {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://webportal;
}
更多回答
优秀答案推荐
I was able to fix the problem by adding absolute_redirect off;
to my location:
我可以通过向我的位置添加绝对重定向OFF;来解决这个问题:
location / {
absolute_redirect off;
return 301 /portal;
}
I struggled with this problem for a bit today. Building on previous answer to extend the solution to kubernetes pod...
今天我在这个问题上挣扎了一会儿。在前面回答的基础上将解决方案扩展到Kubernetes Pod...
Angular app was is deployed behind an nginx server listening on port 80.
It was deployed as a pod called 'ngapp' in a kubernetes-cluster in a namespace called 'frontend'.
deployment.yaml
===============
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ngapp
name: ngapp
spec:
replicas: 1
selector:
matchLabels:
app: ngapp
template:
metadata:
labels:
app: ngapp
spec:
containers:
- image: mydomain.com/ngapp:v1.0.0
name: ngapp
---
apiVersion: v1
kind: Service
metadata:
labels:
app: ngapp
name: ngapp
spec:
type: NodePort
ports:
- name: ngapp-port
port: 4200 #Service Port
targetPort: 80 #Port to forward to
nodePort: 30001 #Port exposed to the external world (laptop browser)
selector:
app: ngapp
This meant the following:
1) fellow containers/pods can access nginx server at ngapp.frontend.svc.cluster.local:4200
(note that this pod is deployed in a namespace called 'frontend',
and hence it is included in the DNS name)
2) host (your laptop browser for instance) can access the nginx server at localhost:30001
3) k8s c-m (container mgr) and core-dns (name->ip resolution) servers in
the control plain of k8s do the magic to send the requests to port 80 on
the 'ngapp' container (where nginx is listening for requests)
- ngapp was build with following command (to enable the base-uri)
ng build --base-href "/ngapp/" -c "k8s"
NG Build--base-href“/ngapp/”-c“K8s”
- The code was deployed at /apps/dist/ngapp/ inside the pod
following is the nginx configuration
以下是nginx配置
All the files referenced here are in ngapp pod.
/etc/nginx/nginx.conf
=====================
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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/ngapp.conf
============================
server {
listen 80;
listen [::]:80;
server_name localhost;
location /ngapp/ {
root /apps/dist;
try_files $uri $uri/ /ngapp/index.html;
}
location / {
absolute_redirect off;
return 301 /ngapp/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
with this setup, I was successfully able to access the SPA website at:
通过此设置,我能够成功访问SPA网站:
http://localhost:30001
http://localhost:30001/
http://localhost:30001/ngapp
http://localhost:30001/ngapp/
更多回答
我是一名优秀的程序员,十分优秀!