繁体   English   中英

NGINX作为反向代理无法显示静态公共文件

[英]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 OK)。 但是不会显示所有静态文件。

静态文件(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,然后将其传递到别名公共路径;您似乎正在获取位置,并且除了关闭“ expires”之外,不对其进行任何操作。

码:

 location ~ ^/(?<dir>[^/]+)/(?<file>[^/]+)$ {

            gzip on;               
            alias /nodeproj/public/$dir/$file;

    }

我发布的配置正确。 解决此问题的方法实际上是更改专业人士上静态文件的根文件夹的所有权。

许可之前:

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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM