静态文件问题 - 使用 nginx 在子目录下托管 PHP 应用程序
Issues with static files - hosting PHP app under subdirectory using nginx
我是现有产品新团队的一员,PO 希望在域下提供演示版本。com/demo。网络服务器有一个由以前的团队配置的 nginx 设置。
我对 nginx 不是很熟悉,但是按照 this 教程来设置演示环境。它似乎工作正常,除了静态文件(图像,css)仍然从生产环境提供。
我们在 /var/www/app/current 和 /var/www/demoapp/current 下使用符号链接的自动部署系统。
nginx 配置如下所示:
server {
server_name www.domain.com;
return 301 $scheme://domain.com$request_uri;
}
server {
server_name domain.com [IP_ADDRESS];
root /var/www/app/current/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js|woff|woff2|ttf|otf|eot)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
location ^~ /demo {
alias /var/www/demoapp/current/public;
try_files $uri $uri/ @demo;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js|woff|woff2|ttf|otf|eot)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
}
location @demo {
rewrite /demo/(.*)$ /demo/index.php?/ last;
}
...gzip & certbot configuration
}
当我通过 SSH 连接到服务器时,我可以看到我们的自动部署系统正在运行,网站上应该可用的新 CSS 编译在 /var/www/demoapp/current/[=36= 下]/css/.但是,当我访问域时。com/demo 它显示与实时版本相同的 CSS 样式表。
演示版在部署到服务器时未更新 PHP 和 HTML 也存在问题。我通过在每次部署时重新加载 FPM 服务来解决这个问题,但我怀疑它也可能与 nginx 配置有关。
如果您能就这些问题中的任何一个提供帮助,我们将不胜感激。
问题最终与 nginx 无关。它是通过更改
的每次出现来修复的
<link href="{{ mix('css/dashboard.css') }}" rel="stylesheet">
至
<link href="{{ asset('css/dashboard.css') }}" rel="stylesheet">
.
我是现有产品新团队的一员,PO 希望在域下提供演示版本。com/demo。网络服务器有一个由以前的团队配置的 nginx 设置。
我对 nginx 不是很熟悉,但是按照 this 教程来设置演示环境。它似乎工作正常,除了静态文件(图像,css)仍然从生产环境提供。
我们在 /var/www/app/current 和 /var/www/demoapp/current 下使用符号链接的自动部署系统。 nginx 配置如下所示:
server {
server_name www.domain.com;
return 301 $scheme://domain.com$request_uri;
}
server {
server_name domain.com [IP_ADDRESS];
root /var/www/app/current/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js|woff|woff2|ttf|otf|eot)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
location ^~ /demo {
alias /var/www/demoapp/current/public;
try_files $uri $uri/ @demo;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js|woff|woff2|ttf|otf|eot)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
}
location @demo {
rewrite /demo/(.*)$ /demo/index.php?/ last;
}
...gzip & certbot configuration
}
当我通过 SSH 连接到服务器时,我可以看到我们的自动部署系统正在运行,网站上应该可用的新 CSS 编译在 /var/www/demoapp/current/[=36= 下]/css/.但是,当我访问域时。com/demo 它显示与实时版本相同的 CSS 样式表。
演示版在部署到服务器时未更新 PHP 和 HTML 也存在问题。我通过在每次部署时重新加载 FPM 服务来解决这个问题,但我怀疑它也可能与 nginx 配置有关。
如果您能就这些问题中的任何一个提供帮助,我们将不胜感激。
问题最终与 nginx 无关。它是通过更改
的每次出现来修复的<link href="{{ mix('css/dashboard.css') }}" rel="stylesheet">
至
<link href="{{ asset('css/dashboard.css') }}" rel="stylesheet">
.