![](/img/trans.png)
[英]Configure Nginx to serve Static files of Angular and also reverse proxy for express
[英]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.