Nginx:创建一个位置以重定向到另一个位置

ql3eal8s  于 2023-10-17  发布在  Nginx
关注(0)|答案(2)|浏览(183)

我试图重定向一个位置到另一个位置,但由于某种原因,它不工作。我的nginx服务在8080:80端口上的Docker container下运行。
我有/portal,显示我的Web应用程序正确,我想从//portal的nginx重定向。我尝试了一些使用location /location = /的方法,但没有成功。
/portal可以通过以下方式正常访问:

http://localhost:8080/portal

但出于某种原因,当我进入

http://localhost:8080/

浏览器重定向到

https://localhost/portal

而不是重定向到:

http://localhost:8080/portal

地点:

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;
}
whhtz7ly

whhtz7ly1#

我可以通过将absolute_redirect off;添加到我的位置来解决这个问题:

location / {
    absolute_redirect off;
    return 301 /portal;
}
72qzrwbm

72qzrwbm2#

今天我就这个问题纠结了一下。基于前面的答案,将解决方案扩展到kubernetes pod.

  1. Angular app部署在Nginx服务器后面,监听端口80。
    1.它被部署为一个名为'ngapp'的pod,位于一个名为'frontend'的命名空间中的kubernetes-cluster中。
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)
  1. ngapp是使用以下命令构建的(以启用base-uri)
    ng build --base-href“/ngapp/”-c“k8s”
    1.代码部署在pod中的/apps/dist/ngapp/
    下面是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;
    }
}

通过此设置,我成功地访问了SPA网站:

http://localhost:30001
http://localhost:30001/
http://localhost:30001/ngapp
http://localhost:30001/ngapp/

相关问题