0

我正在尝试使用nginx 在端口 3000 上本地使用Prerenderphantom.js

/assets/*我知道如果我通过网络服务器代理请求,所有相对 URL 都应该可以正常工作,但是只有当我Prerender phantomjs在 nginx 中添加配置时,我才能访问其中的一些文件。

assets 文件夹中存在但我无法 icomoon-45c69cd6a8.woff2通过浏览器访问该文件的文件示例:

ubuntu:~/Desktop/prerender$ ll /var/app/assets/fonts/icomoon-45c69cd6a8.woff2
-rwxrwxr-x 1 64116 Aug 20 12:45 /var/app/assets/fonts/icomoon-45c69cd6a8.woff2*

ubuntu:~/Desktop/prerender$ ll /var/app/assets/build/app/vendor-c3ca43539c.js 
-rw-rw-r-- 1 969588 Jul 10 16:42 /var/app/assets/build/app/vendor-c3ca43539c.js

错误:

2017/08/24 15:02:40 [notice] 20882#20882: *1 "_escaped_fragment_" does not match "", client: 127.0.0.1, server: domain.info, request: "GET /assets/fonts/icomoon-45c69cd6a8.woff2 HTTP/1.1", host: "domain.info"
2017/08/24 15:02:40 [notice] 20882#20882: *1 "Prerender" does not match "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", client: 127.0.0.1, server: domain.info, request: "GET /assets/fonts/icomoon-45c69cd6a8.woff2 HTTP/1.1", host: "domain.info"
2017/08/24 15:02:40 [notice] 20882#20882: *1 "Chrome|chrome" matches "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", client: 127.0.0.1, server: domain.info, request: "GET /assets/fonts/icomoon-45c69cd6a8.woff2 HTTP/1.1", host: "domain.info"
2017/08/24 15:02:40 [notice] 20882#20882: *1 "^(/home(/index)?|/index(.php)?)/?$" does not match "/assets/fonts/icomoon-45c69cd6a8.woff2", client: 127.0.0.1, server: domain.info, request: "GET /assets/fonts/icomoon-45c69cd6a8.woff2 HTTP/1.1", host: "domain.info"
2017/08/24 15:02:40 [notice] 20882#20882: *1 "index/?$" does not match "/assets/fonts/icomoon-45c69cd6a8.woff2", client: 127.0.0.1, server: domain.info, request: "GET /assets/fonts/icomoon-45c69cd6a8.woff2 HTTP/1.1", host: "domain.info"
2017/08/24 15:02:40 [notice] 20882#20882: *1 ".*" matches "/assets/fonts/icomoon-45c69cd6a8.woff2", client: 127.0.0.1, server: domain.info, request: "GET /assets/fonts/icomoon-45c69cd6a8.woff2 HTTP/1.1", host: "domain.info"
2017/08/24 15:02:40 [notice] 20882#20882: *1 rewritten data: "/https://domain.info/assets/fonts/icomoon-45c69cd6a8.woff2", args: "", client: 127.0.0.1, server: domain.info, request: "GET /assets/fonts/icomoon-45c69cd6a8.woff2 HTTP/1.1", host: "domain.info

Nginx 配置:

server {
       listen         80;
       server_name    www.domain.info domain.info;
       return         301 https://domain.info$request_uri;
}

server {

    #listen 80;
    listen 443 ssl;
    listen [::]:443 http2 ipv6only=on;
    server_name domain.info;
    root /var/app;
    ssl                  on;
    ssl_certificate      /etc/nginx/ssl/domain.info.crt;
    ssl_certificate_key  /etc/nginx/ssl/domain.info.key;
    ssl_session_cache shared:SSL:20m;
    ssl_session_timeout 180m;
    ssl_protocols  SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    index index.php index.html index.htm;
    if ($args ~ "_escaped_fragment_") {
        set $prerender 1;
    }
    if ($http_user_agent ~ "Prerender") {
        set $prerender 0;
    }
    if ($http_user_agent ~ "Chrome|chrome") {
      set $prerender 1;
    }

    location / {

        if ($prerender = 1) {
            #try_files $uri $uri/ /index.php?$args;
            rewrite .* /$scheme://$host$request_uri? break;
            proxy_pass http://localhost:3000;
        }

        try_files $uri $uri/ /index.php?$args;  
    }

    # Canonicalize Codeigniter URL endpoints If the default controller is other than "welcome" change accordingly the following rules
    if ($request_uri ~* ^(/home(/index)?|/index(.php)?)/?$) {
        rewrite ^(.*)$ / permanent;
    }

    # Removes trailing "index" from all controllers
    if ($request_uri ~* index/?$) {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # Return to index.php when accessing non-existing php files
    error_page 404 /index.php;

    location ~ \.php$ {

        # try_files $uri = /index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$; 
        fastcgi_pass unix:/var/run/php7.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_keep_conn off;
        proxy_read_timeout     3000;
        proxy_connect_timeout  3000;
        fastcgi_buffering on;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 16k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_read_timeout 3600;
    }

    location ~* \.(gif|ico|css|js|svg|ttf)$ {
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public";
    }

    location ~ /\.(htaccess|htpasswd|bak|txt)$ {
        deny all;
        access_log off;
        log_not_found off;
    }

    location /health_check {
            try_files $uri $uri/ /index.php?$args;
    }

    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_comp_level  9;
    gzip_min_length  10000;    
    gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript application/json text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
}
4

0 回答 0