С Varnish: 80 и Nginx: 8080 веб-сайт 404 не найден

Я следовал нескольким руководствам как настроить Varnish (например, https://www.globo.tech/learning-center/set-up-varnish-ubuntu-16/ ), и все они говорят установить Varnish на порт 80 и Nginx на 8080.

Вот что мы видим в netstat -nlpt :

tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      12465/nginx.conf
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12059/varnishd
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      12465/nginx.conf
tcp        0      0 127.0.0.1:6082          0.0.0.0:*               LISTEN      12059/varnishd
tcp6       0      0 :::80                   :::*                    LISTEN      12059/varnishd
tcp6       0      0 ::1:6082                :::*                    LISTEN      12059/varnishd

Это на 100% полный nginx .config моего сайта:

server {
  listen 127.0.0.1:8080;
  server_name example.com www.example.com;
  #return 301 https://www.example.com$request_uri; <--- had to remove this redirect because of the addition to default.vcl, with both enabled it gives infinite redirect loop.
}
server {
  listen 443;
  server_name example.com;
  return 301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl http2;

  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers 'EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5';
  ssl_dhparam /etc/nginx/ssl/dhparams.pem;
  ssl_session_timeout 1d;
  ssl_session_cache shared:SSL:50m;
  ssl_stapling on;
  ssl_stapling_verify on;
  add_header Strict-Transport-Security max-age=15768000;


  root /var/www/example.com/htdocs;

  server_name example.com www.example.com;

  location / {
    #autoindex on;
    #try_files $uri $uri/ =404;

    proxy_pass http://127.0.0.1:80;
    proxy_read_timeout    90;
    proxy_connect_timeout 90;
    proxy_redirect        off;

    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-Port 443;
    proxy_set_header Host $host;

  }

  location ~* /img/.*\.gif$ {
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public";
  }
}

Если я перейду на example.com , не просто перенаправления на https и www не работают, я просто получаю 404 not found. И все, что я сделал в своем .conf, это изменил 80 на 8080 в верхней строке.

Также Varnish кэширует пример 404 curl --head -I.com

HTTP/1.1 404 Not Found
Server: nginx/1.11.10
Date: Tue, 21 Feb 2017 11:01:56 GMT
Content-Type: text/html
Content-Length: 170
X-Varnish: 72 65
Age: 340
Via: 1.1 varnish-v4
Cache-Tags: HIT
Connection: keep-alive

И чтобы загрузить веб-сайт, мне нужно ввести www .example.com, он перенаправляет на https://www.example.com , сайт загружается, но Varnish не кэширование curl --head -I https://www.example.com :

HTTP/1.1 200 OK
Server: nginx/1.11.10
Date: Tue, 21 Feb 2017 11:09:08 GMT
Content-Type: text/html
Content-Length: 478
Last-Modified: Fri, 17 Feb 2017 17:30:11 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "58a73323-1de"
Strict-Transport-Security: max-age=15768000
Accept-Ranges: bytes

Также varnishd выдает Ошибка: Невозможно открыть сокет:: 80: Адрес уже при использовании

varnishstat выглядит хорошо:

MAIN.uptime                                                                                                                       0+00:30:07
MAIN.sess_conn                                                                                                                            44          0.00           .            0.00          0.00          0.00
MAIN.client_req                                                                                                                           74          0.00           .            0.00          0.00          0.00
MAIN.cache_hit                                                                                                                            70          0.00           .            0.00          0.00          0.00
MAIN.cache_miss                                                                                                                            4          0.00           .            0.00          0.00          0.00
MAIN.backend_reuse                                                                                                                         3          0.00           .            0.00          0.00          0.00
MAIN.backend_recycle                                                                                                                       6          0.00           .            0.00          0.00          0.00
MAIN.fetch_length                                                                                                                          6          0.00           .            0.00          0.00          0.00
MAIN.pools                                                                                                                                 2          0.00           .            2.00          2.00          2.00
MAIN.threads                                                                                                                             200          0.00           .          200.00        200.00        200.00
MAIN.threads_created                                                                                                                     200          0.00           .            0.00          0.00          0.00
MAIN.n_object                                                                                                                              4          0.00           .            4.00          4.00          4.00
MAIN.n_objectcore                                                                                                                          5          0.00           .            5.00          5.00          5.00
MAIN.n_objecthead                                                                                                                          6          0.00           .            6.00          6.00          6.00
MAIN.n_backend                                                                                                                             1          0.00           .            1.00          1.00          1.00
MAIN.s_sess                                                                                                                               44          0.00           .            0.00          0.00          0.00
MAIN.s_req                                                                                                                                74          0.00           .            0.00          0.00          0.00
MAIN.s_fetch                                                                                                                               4          0.00           .            0.00          0.00          0.00
MAIN.s_req_hdrbytes                                                                                                                    24.54K         0.00         13.00          0.00          0.00          0.00
MAIN.s_resp_hdrbytes                                                                                                                   15.99K         0.00          9.00          0.00          0.00          0.00
MAIN.s_resp_bodybytes                                                                                                                  11.46K         0.00          6.00          0.00          0.00          0.00
MAIN.backend_req                                                                                                                           6          0.00           .            0.00          0.00          0.00
MAIN.n_vcl                                                                                                                                 1          0.00           .            0.00          0.00          0.00
MAIN.bans                                                                                                                                  1          0.00           .            1.00          1.00          1.00
MAIN.vmods                                                                                                                                 2          0.00           .            2.00          2.00          2.00
MGT.uptime                                                                                                                        0+00:30:07
SMA.s0.c_req                                                                                                                              12          0.00           .            0.00          0.00          0.00
SMA.s0.c_bytes                                                                                                                          2.68K         0.00          1.00          0.00          0.00          0.00
SMA.s0.c_freed                                                                                                                           908          0.00           .            0.00          0.00          0.00
SMA.s0.g_alloc                                                                                                                             8          0.00           .            8.00          8.00          8.00
SMA.s0.g_bytes                                                                                                                          1.80K         0.00           .            1.80K         1.80K         1.80K
SMA.s0.g_space                                                                                                                        256.00M         0.00           .          256.00M       256.00M       256.00M
VBE.boot.default.bereq_hdrbytes                                                                                                         1.61K         0.00           .            0.00          0.00          0.00
VBE.boot.default.beresp_hdrbytes                                                                                                         936          0.00           .            0.00          0.00          0.00
VBE.boot.default.beresp_bodybytes                                                                                                       1020          0.00           .            0.00          0.00          0.00
VBE.boot.default.conn                                                                                                                      6          0.00           .            6.00          6.00          6.00
VBE.boot.default.req                                                                                                                       6          0.00           .            0.00          0.00          0.00

ОБНОВЛЕНИЕ:

Далее следуют https://www.smashingmagazine.com/2015/09/https-everywhere-with-nginx-varnish-apache/ и добавил их в config:

proxy_pass http://127.0.0.1:80;
proxy_read_timeout    90;
proxy_connect_timeout 90;
proxy_redirect        off;

proxy_set_header X-Real-IP  $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header Host $host;

и эти в .vcl

# handles redirecting from http to https
sub vcl_synth {
  if (resp.status == 750) {
    set resp.status = 301;
    set resp.http.Location = req.http.x-redir;
    return(deliver);
  }
}

if ( (req.http.host ~ "^(?i)smashing_ssl_one.tutorials.eoms") && req.http.X-Forwarded-Proto !~ "(?i)https") {
  set req.http.x-redir = "https://" + req.http.host + req.url;
  return (synth(750, ""));
}

По-прежнему 404, но, по крайней мере, varnish кэширует https: // сейчас.

curl -I https://www.example.com/
HTTP/1.1 404 Not Found
Server: nginx/1.11.10
Date: Tue, 21 Feb 2017 12:58:47 GMT
Content-Type: text/html
Content-Length: 170
Connection: keep-alive
X-Varnish: 32849 32847
Age: 63
Via: 1.1 varnish-v4
X-Cache: HIT

Также, хотя https://www.example.com/ - это 404, не найдено, но, например, https://www.example.com/img/pic.png обнаружен и загружает элемент, но не покрыт лаком.

0
задан 21 February 2017 в 15:12
1 ответ

Используйте приведенную ниже конфигурацию nginx для прокси-сервера 443

server {
        listen 443 ssl;

        server_name www.example.com example.com;

       ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
       ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
       ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
       ssl_ciphers         HIGH:!aNULL:!MD5;

        location / {
            proxy_pass http://127.0.0.1:80;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-Port 443;
            proxy_set_header Host $host;
            proxy_connect_timeout       86400;
            proxy_send_timeout          86400;
            proxy_read_timeout          86400;
            send_timeout                86400;
            client_max_body_size 50m;
        client_body_buffer_size 16k;
}
        location /.git {
            deny all;
            return 404;
        }
}
0
ответ дан 23 March 2020 в 14:19

Теги

Похожие вопросы