nginx 不提供 JS, CSS 文件用于 PHP app behind ingress-nginx
nginx not serving JS, CSS files for PHP app behind ingress-nginx
似乎无法正常工作,需要帮助解决我哪里出错了。
有一个旧的 PHP 应用在 /admin
的子路径中。 ingress-nginx
将它的流量转发到 Pod 中的 nginx
服务器 运行。我已经验证我可以执行以下操作并且它可以正确地提供资产:
kubectl port-forward <admin-pod> 4000:4000
skaffold dev --port-forward
docker run -p 4000:4000 <image>
但是,当我尝试通过 minikube ip
、192.168.64.5/admin
等浏览器连接访问它时,我只得到以下信息:
它显示 HTML,但是 none 的资产正在加载,因为找不到它们,我不确定为什么。
这是为 PHP 应用程序服务的 nginx
default.conf
。如果可能的话,我想在这里修复它,而不是在 ingress.yaml
中,因为它们往往会扰乱我的其他路线,并花了我一些时间才能使它们正常工作。
这是我对这些文件的看法:
# default.conf
server {
listen 4000;
# rewrite ^([^.]*[^/])$ / permanent;
root /usr/share/nginx/html/src;
include /etc/nginx/default.d/*.conf;
index app.php index.php index.html index.htm;
client_max_body_size 500m;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
# ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/proxy-body-size: "0"
nginx.org/client-max-body-size: "500m"
name: ingress-service-dev
namespace: default
spec:
rules:
- http:
paths:
- path: /adminv2/
backend:
serviceName: admin-new-cluster-ip-service-dev
servicePort: 4001
- path: /admin/
backend:
serviceName: admin-old-cluster-ip-service-dev
servicePort: 4000
- path: /api/
backend:
serviceName: api-cluster-ip-service-dev
servicePort: 5000
- path: /
backend:
serviceName: client-cluster-ip-service-dev
servicePort: 3000
# Dockerfile
FROM php:7.3-fpm
# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS -std=c++11"
RUN apt-get update \
&& apt-get install -y nginx \
&& apt-get install -y libpq-dev zlib1g-dev libzip-dev \
&& docker-php-ext-install pgsql zip mbstring opcache
RUN { \
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=2'; \
echo 'opcache.fast_shutdown=1'; \
echo 'opcache.enable_cli=1'; \
} > /usr/local/etc/php/conf.d/php-opocache-cfg.ini
COPY . /usr/share/nginx/html
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
COPY ./conf/default.conf /etc/nginx/conf.d/default.conf
COPY ./conf/entrypoint.sh /etc/entrypoint.sh
WORKDIR /usr/share/nginx/html/src
RUN composer install
# COPY --chown=www-data:www-data . /app/src
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN mv "/usr/share/nginx/html/conf/file_size.ini" "$PHP_INI_DIR/conf.d/"
# EXPOSE 4000
ENTRYPOINT ["sh", "/etc/entrypoint.sh"]
# project structure
app-root/
admin-new/
admin-old/
conf/
company.conf
src/
css/
js/
Templates/
index.tbs
index.php
Dockerfile.dev
api/
client/
manifests/
# docker structure
/app
conf/
company.conf
src/
css/
js/
Templates/
index.tbs
index.php
Dockerfile.dev
感谢您的帮助。
通过将此路径移动到单独的入口配置,设法让所有路由正常工作。可能不理想,但我只需要它能正常工作,直到它在 6 个月内被更换。
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/proxy-body-size: "0"
nginx.org/client-max-body-size: "500m"
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^(/admin)$ / permanent;
name: ingress-service-dev-admin
namespace: default
spec:
rules:
- http:
paths:
- path: /admin/?(.*)
backend:
serviceName: admin-old-cluster-ip-service-dev
servicePort: 4000
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^(/new-admin)$ / permanent;
name: ingress-service-dev
namespace: default
spec:
rules:
- http:
paths:
- path: /new-admin/
backend:
serviceName: admin-new-cluster-ip-service-dev
servicePort: 4001
- path: /api/
backend:
serviceName: api-cluster-ip-service-dev
servicePort: 5000
- path: /
backend:
serviceName: client-cluster-ip-service-dev
servicePort: 3000
似乎无法正常工作,需要帮助解决我哪里出错了。
有一个旧的 PHP 应用在 /admin
的子路径中。 ingress-nginx
将它的流量转发到 Pod 中的 nginx
服务器 运行。我已经验证我可以执行以下操作并且它可以正确地提供资产:
kubectl port-forward <admin-pod> 4000:4000
skaffold dev --port-forward
docker run -p 4000:4000 <image>
但是,当我尝试通过 minikube ip
、192.168.64.5/admin
等浏览器连接访问它时,我只得到以下信息:
它显示 HTML,但是 none 的资产正在加载,因为找不到它们,我不确定为什么。
这是为 PHP 应用程序服务的 nginx
default.conf
。如果可能的话,我想在这里修复它,而不是在 ingress.yaml
中,因为它们往往会扰乱我的其他路线,并花了我一些时间才能使它们正常工作。
这是我对这些文件的看法:
# default.conf
server {
listen 4000;
# rewrite ^([^.]*[^/])$ / permanent;
root /usr/share/nginx/html/src;
include /etc/nginx/default.d/*.conf;
index app.php index.php index.html index.htm;
client_max_body_size 500m;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
# ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/proxy-body-size: "0"
nginx.org/client-max-body-size: "500m"
name: ingress-service-dev
namespace: default
spec:
rules:
- http:
paths:
- path: /adminv2/
backend:
serviceName: admin-new-cluster-ip-service-dev
servicePort: 4001
- path: /admin/
backend:
serviceName: admin-old-cluster-ip-service-dev
servicePort: 4000
- path: /api/
backend:
serviceName: api-cluster-ip-service-dev
servicePort: 5000
- path: /
backend:
serviceName: client-cluster-ip-service-dev
servicePort: 3000
# Dockerfile
FROM php:7.3-fpm
# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS -std=c++11"
RUN apt-get update \
&& apt-get install -y nginx \
&& apt-get install -y libpq-dev zlib1g-dev libzip-dev \
&& docker-php-ext-install pgsql zip mbstring opcache
RUN { \
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=2'; \
echo 'opcache.fast_shutdown=1'; \
echo 'opcache.enable_cli=1'; \
} > /usr/local/etc/php/conf.d/php-opocache-cfg.ini
COPY . /usr/share/nginx/html
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
COPY ./conf/default.conf /etc/nginx/conf.d/default.conf
COPY ./conf/entrypoint.sh /etc/entrypoint.sh
WORKDIR /usr/share/nginx/html/src
RUN composer install
# COPY --chown=www-data:www-data . /app/src
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN mv "/usr/share/nginx/html/conf/file_size.ini" "$PHP_INI_DIR/conf.d/"
# EXPOSE 4000
ENTRYPOINT ["sh", "/etc/entrypoint.sh"]
# project structure
app-root/
admin-new/
admin-old/
conf/
company.conf
src/
css/
js/
Templates/
index.tbs
index.php
Dockerfile.dev
api/
client/
manifests/
# docker structure
/app
conf/
company.conf
src/
css/
js/
Templates/
index.tbs
index.php
Dockerfile.dev
感谢您的帮助。
通过将此路径移动到单独的入口配置,设法让所有路由正常工作。可能不理想,但我只需要它能正常工作,直到它在 6 个月内被更换。
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/proxy-body-size: "0"
nginx.org/client-max-body-size: "500m"
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^(/admin)$ / permanent;
name: ingress-service-dev-admin
namespace: default
spec:
rules:
- http:
paths:
- path: /admin/?(.*)
backend:
serviceName: admin-old-cluster-ip-service-dev
servicePort: 4000
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^(/new-admin)$ / permanent;
name: ingress-service-dev
namespace: default
spec:
rules:
- http:
paths:
- path: /new-admin/
backend:
serviceName: admin-new-cluster-ip-service-dev
servicePort: 4001
- path: /api/
backend:
serviceName: api-cluster-ip-service-dev
servicePort: 5000
- path: /
backend:
serviceName: client-cluster-ip-service-dev
servicePort: 3000