大网数据广告活动
服务器运维问答

How to use varnish with nginx as SSL terminator and with http to https forwarding with nginx

· 52CE 我爱测

I have installed varnish and nginx and secured my website with lets encrypt SSL. I tested varnish against nginx cgi cache and i have found out that varnish is a bit faster in my use case (wordpress). So i need some help on how to…

问题原文

原帖链接 · Rahul Biswas · CC BY-SA 4.0

I have installed varnish and nginx and secured my website with lets encrypt SSL. I tested varnish against nginx cgi cache and i have found out that varnish is a bit faster in my use case (wordpress). So i need some help on how to use nginx for SSL termination with varnish and how to forward http to https as my website is https only. I am running ubuntu 18.04 Please help me someone!!

EDIT: I followed this guide: https://www.linode.com/docs/websites/varnish/use-varnish-and-nginx-to-serve-wordpress-over-ssl-and-http-on-debian-8/

It gives my error in nginx (port 80 already in use)

Please help me!

已采纳解答

原帖链接 · Thijs Feryn · CC BY-SA 4.0

Assumptions

  • Nginx has a vhost running on port 443 to process incoming HTTPS requests
  • Nginx has a vhost running on port 80 to process incoming HTTP request and redirect them to Varnish
  • Varnish is running on port 6081 and sits behind the HTTPS Nginx vhost
  • Nginx will probably also have a vhost running on port 8080 which does no proxying, but acts as the actual webserver, serving files or PHP requests.

As you can see, my setup differs a little from the one in https://www.linode.com/docs/websites/varnish/use-varnish-and-nginx-to-serve-wordpress-over-ssl-and-http-on-debian-8/. That's because I already tackle HTTP to HTTPS redirection using a separate Nginx vhost.

In this case, Varnish is not listening on port 80, but on 6081.

HTTPS vhost

Here's an example of an Nginx vhost that processes the HTTPS connections:

server {
    listen 443 ssl;
    keepalive_timeout   70;
    server_name example.com www.example.com;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_certificate ssl.crt;
    ssl_certificate_key ssl.key;
    ssl_session_cache   shared:SSL:20m;
    ssl_session_timeout 4h;
    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://127.0.0.1:6081;
        proxy_http_version 1.1;
    }
}

HTTP to HTTPS redirection vhost

Here's a snippet that redirects HTTP to HTTPS:

server {
        server_name example.com www.example.com;
        listen  80;
        rewrite "^/$" https://example.com permanent;
        rewrite "^/(.+)$" http://example.com/$1 permanent;
}

Redacted content

The vhosts have been redacted and contain example values. Please replace the hostnames accordingly in the server_name expressions.

Please also make sure ssl_certificate and ssl_certificate_key statements point to files that actually exist.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-09-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。