Nginx 删除 uri 中的路径



我需要删除请求 uri 中的部分路径。我正在使用Goocle Cloud上的入口来访问服务器,我希望如果我转到localhost/symfony/,则在uri请求中忽略symfony路径。

目前,如果我去localhost/symfony,它可以工作,但如果我去localhost/symfony/something,我会收到404默认后端。 我也尝试添加 1 美元来设置request_url但它不起作用。

这是我的配置:

apiVersion: apps/v1
kind: Deployment
metadata:
name: symfony-nginx
namespace: default
labels:
app: symfony-nginx
spec:
replicas: 1
selector:
matchLabels:
app: symfony-nginx
template:
metadata:
labels:
app: symfony-nginx
spec:
volumes:
- name: symfony-nginx
configMap:
name: symfony-nginx-configmap
containers:
- name: symfony-nginx
image: nginx:stable
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/conf.d
name: symfony-nginx
resources:
requests:
memory: 32Mi
cpu: 10m
limits:
memory: 64Mi
cpu: 30m
readinessProbe:
httpGet:
path: /nginx-health
port: 80
initialDelaySeconds: 15
timeoutSeconds: 1
periodSeconds: 10
livenessProbe:
httpGet:
path: /nginx-health
port: 80
initialDelaySeconds: 15
timeoutSeconds: 1
periodSeconds: 10
---
apiVersion: v1
kind: ConfigMap
metadata:
name: symfony-nginx-configmap
namespace: default
data:
default.conf: |
server {
server_name php-docker.local;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/symfony/public;
proxy_buffering off;
location = /nginx-health {
access_log off;
return 200 "healthyn";
}
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index.php(/|$) {
set $request_url $request_uri;
if ($request_uri ~ ^/symfony(.*)$ ) {
set $request_url /;
}
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass symfony:9000;
include fastcgi_params;
fastcgi_param  REQUEST_URI $request_url;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $document_root;
internal;
}
location ~ .php$ {
return 404;
}
}
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: symfony-nginx-hpa
namespace: default
labels:
app: symfony-nginx
spec:
scaleTargetRef:
kind: Deployment
name: symfony-nginx
apiVersion: apps/v1
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 70
---
apiVersion: v1
kind: Service
metadata:
name: symfony-nginx-np
namespace: default
labels:
app: symfony-nginx
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: symfony-nginx
type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: symfony-nginx-ingress
spec:
rules:
- http:
paths:
- path: /symfony
backend:
serviceName: symfony-nginx-np
servicePort: 80

有什么建议吗?

非常感谢

帕斯夸莱

您需要在入口对象中使用注释来重写 URI。nginx.ingress.kubernetes.io/rewrite-target注释以必须重定向流量的 URI 为目标。例:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
name: symfony-nginx-ingress
spec:
rules:
- http:
paths:
- path: /symfony
backend:
serviceName: symfony-nginx-np
servicePort: 80

在这种情况下,它会将 URI 从localhost/symfony/重写为localhost/

请注意,它适用于入口对象中描述的所有终结点。

最新更新