NGINX 作为反向代理无法显示静态 public 文件
NGINX as reverse proxy cannot display static public files
我的本地网络上有 2 台 linux 服务器。我使用 IP 为 192.168.1.111 的 PC 作为应用程序服务器 运行 我在端口 8080 上的节点应用程序。作为 Web 服务器,我在 IP 为 192.168.1.100 的 PC 上使用 NGINX 并将其配置为反向代理。
在浏览器的“网络”选项卡中,我看到所有文件都已正常提供(状态 200 正常)。但是没有显示所有静态文件。
静态文件(css、js、图像、字体)位于 /var/www/domain.com/public
内的子文件夹中
知道为什么会出现这个问题吗?
这是我的 nginx.conf
文件:
user www-data;
worker_processes 2;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/domain.com/access.log;
error_log /var/log/nginx/domain.com/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
这里是/etc/nginx/sites-enabled/domain.com
文件
upstream appserver {
server 192.168.1.111:8080;
}
server {
listen 80;
server_name domain.com www.domain.com;
root /var/www/domain.com/public;
location ~ ^/(images/|js/|css/|media/|favicon.ico) {
#access_log off;
expires off;
}
location / {
proxy_pass http://appserver;
include /etc/nginx/proxy_params;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
这里是 access.log
文件
192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/shop.png HTTP/1.1" 200 13643 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/code.png HTTP/1.1" 200 13443 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/line.png HTTP/1.1" 200 13643 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
我不太擅长正则表达式和 nginx,但我就是这样做的,而且对我很有用。
基本上获取发送到 get url 的目录和文件路径,然后将其传递给别名 public 路径;您似乎正在获取位置,但除了关闭 [= =17=]。
代码:
location ~ ^/(?<dir>[^/]+)/(?<file>[^/]+)$ {
gzip on;
alias /nodeproj/public/$dir/$file;
}
我发布的配置是正确的。此问题的修复实际上是更改 pro 上静态文件的根文件夹的所有权。
之前的权限:
drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 css/
drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 fonts/
drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 images/
drwxr-xr-x 2 www-data root 4096 Jan 26 16:17 js/
webmaster@proxy:~$
(这有效)之后的许可:
drwxrwxr-x 2 webmaster webmaster 4096 Jan 26 00:00 css/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 13 00:15 fonts/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 13 00:15 images/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 26 16:50 js/
webmaster@proxy:~$
要更改所有权并设置正确的权限运行,请执行以下命令:
sudo chmod 775 /var/www/domain.com/public/ -R
sudo chown -R webmaster:webmaster /var/www/domain.com/public/
我的本地网络上有 2 台 linux 服务器。我使用 IP 为 192.168.1.111 的 PC 作为应用程序服务器 运行 我在端口 8080 上的节点应用程序。作为 Web 服务器,我在 IP 为 192.168.1.100 的 PC 上使用 NGINX 并将其配置为反向代理。
在浏览器的“网络”选项卡中,我看到所有文件都已正常提供(状态 200 正常)。但是没有显示所有静态文件。
静态文件(css、js、图像、字体)位于 /var/www/domain.com/public
知道为什么会出现这个问题吗?
这是我的 nginx.conf
文件:
user www-data;
worker_processes 2;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/domain.com/access.log;
error_log /var/log/nginx/domain.com/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
这里是/etc/nginx/sites-enabled/domain.com
文件
upstream appserver {
server 192.168.1.111:8080;
}
server {
listen 80;
server_name domain.com www.domain.com;
root /var/www/domain.com/public;
location ~ ^/(images/|js/|css/|media/|favicon.ico) {
#access_log off;
expires off;
}
location / {
proxy_pass http://appserver;
include /etc/nginx/proxy_params;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
这里是 access.log
文件
192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/shop.png HTTP/1.1" 200 13643 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/code.png HTTP/1.1" 200 13443 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/line.png HTTP/1.1" 200 13643 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
我不太擅长正则表达式和 nginx,但我就是这样做的,而且对我很有用。 基本上获取发送到 get url 的目录和文件路径,然后将其传递给别名 public 路径;您似乎正在获取位置,但除了关闭 [= =17=]。
代码:
location ~ ^/(?<dir>[^/]+)/(?<file>[^/]+)$ {
gzip on;
alias /nodeproj/public/$dir/$file;
}
我发布的配置是正确的。此问题的修复实际上是更改 pro 上静态文件的根文件夹的所有权。
之前的权限:
drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 css/
drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 fonts/
drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 images/
drwxr-xr-x 2 www-data root 4096 Jan 26 16:17 js/
webmaster@proxy:~$
(这有效)之后的许可:
drwxrwxr-x 2 webmaster webmaster 4096 Jan 26 00:00 css/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 13 00:15 fonts/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 13 00:15 images/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 26 16:50 js/
webmaster@proxy:~$
要更改所有权并设置正确的权限运行,请执行以下命令:
sudo chmod 775 /var/www/domain.com/public/ -R
sudo chown -R webmaster:webmaster /var/www/domain.com/public/