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

问题描述 投票:0回答:2

我正在尝试将一个位置重定向到另一个位置,但由于某种原因它不起作用。我的 nginx 服务正在 8080:80 端口上的

Docker 容器
下运行。

我有

/portal
可以正确显示我的 Web 应用程序,我希望 nginx 从
/
重定向到
/portal
。我尝试过使用
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;
}
nginx nginx-reverse-proxy nginx-location
2个回答
0
投票

我可以通过将

absolute_redirect off;
添加到我的位置来解决问题:

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

0
投票

今天我为这个问题苦苦挣扎了一会儿。以之前的答案为基础,将解决方案扩展到 kubernetes pod...

  1. Angular 应用程序部署在监听端口 80 的 nginx 服务器后面。

  2. 它被部署为 kubernetes 集群中名为“frontend”的命名空间中名为“ngapp”的 pod。

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/
© www.soinside.com 2019 - 2024. All rights reserved.