如何让nginx充当代理和Web服务器?

时间:2015-11-17 20:32:22

标签: apache nginx reverse-proxy varnish

我已将nginx设置为带有apache后端的varnish的ssl反向代理,并且它工作正常,但现在我需要nginx充当一个目录的Web服务器。

server {
    listen 10.0.0.20:80;
    server_name example.com www.example.com;
    return 301 https://www.example.com$request_uri;
}


server {
    listen 10.0.0.20:443 ssl spdy backlog=16384;
        server_name example.com www.example.com;
        server_name_in_redirect off;
        ssl    on;
        ssl_certificate    /home/ubuntu/mydomain-ssl/mydomain.crt;
        ssl_certificate_key    /home/ubuntu/mydomain-ssl/mydomain.key;
        ssl_dhparam /home/ubuntu/mydomain-ssl/dh2048.pem;
        ssl_prefer_server_ciphers on;
        ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_cache shared:SSL:2m;
        ssl_session_timeout 5m;
        ssl_stapling on;
        ssl_stapling_verify on;
        add_header Public-Key-Pins 'pin-sha256="gFcziPQKxI10QgFcziPQKxI10QgFcziPQKxI10QgFcziPQKxI10Q"; pin-sha256="gFcziPQKxI10QgFcziPQKxI10QgFcziPQKxI10QgFcziPQKxI10Q";  max-age=5184000; includeSubDomains';
        add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
        add_header X-Frame-Options SAMEORIGIN;
        add_header  X-Content-Type-Options "nosniff";
        add_header X-XSS-Protection "1; mode=block";

        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_headers_hash_max_size 1024;
            proxy_headers_hash_bucket_size 128;
            proxy_pass http://127.0.0.1:80;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-Port 443;
            proxy_set_header X-Forwarded-Protocol $scheme;
        }
}

如果有人可以展示一个简单的示例,说明如何提供目录/目录/作为此配置的一部分,这将是非常有用的,我尝试通过添加其他位置块进行配置,但它不起作用,我刚刚得到访问目录时出现404错误。

1 个答案:

答案 0 :(得分:0)

您可以拥有一个默认位置,可以将流量重定向到https,以及一个仅提供文件的更具体位置:

server {
    listen 10.0.0.20:80;
    server_name example.com www.example.com;

    # absolute path to your directory
    root /var/www/www.example.com/directory;

    location / {
        return 301 https://www.example.com$request_uri; 
    }

    location /directory/ {
        try_files $uri $uri/ =404;
    }
}