我有一个带有Ingress的k8s应用程序,我想用它来使用子域服务不同的应用程序。
这是我的完整配置:
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:latest
imagePullPolicy: Always
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 "healthy\n";
}
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass symfony:9000;
include fastcgi_params;
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
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
我只收到'没有为“GET / symfony”找到的路由,因为到达php-fpm的uri保留了'symfony'前缀。
如何在'symfony'之后将NGINX更改为仅发送到php-fpm的uri部分?
[UPDATE]
也许这是一个symfony应用程序问题,因为我在http://mydomanin.com/symfony上获得了index.php的响应,并且找到了“找不到GET / symfony”的路由?或者这是NGINX的责任?
谢谢,问候
最好的解决方案是为交响乐本身的所有路线设置/symphony
前缀。
另一种方法是在nginx上使用rewrite
:
rewrite ^/symphony/(.*)$ /$1 last;
但后来是后果! Symphony将返回带有'/myImage.jpg'的图像URL,对于js / css /同样...链接将是''等等,这些页面将不会映射到您的应用程序。
最后我找到了解决方案。
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: symfony-nginx-ingress
spec:
rules:
- http:
paths:
- path: /symfony
backend:
serviceName: symfony-nginx-np
servicePort: 80
这个Ingress配置只匹配/ symfony路由,所以要完成它你还需要添加/ symfony / * ......我认为这不是很优雅......