新站提交
热搜: 目录平台

动静分离

通常我们拥有动态处理服务器(例如php、php-fpm、Python wsgi,Node.JS等),而静态文件我们又希望使用Nginx来管理,提供缓存等功能。那我们用到如下设置:

location ~ .*.(htm|html|js|css|gif|jpg|jpeg|png|bmp|ico|swf|flv)$        {                root /data/www;        } location / {              proxy_pass http://10.0.1.110:8000;              proxy_redirect off;              proxy_set_header HOST $host;              proxy_set_header X-Real-IP $remote_addr;              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              client_max_body_size 10m;              client_body_buffer_size 128k;              proxy_connect_timeout 90;              proxy_send_timeout 90;              proxy_read_timeout 90;              proxy_buffer_size 4k;              proxy_buffers 4 32k;              proxy_busy_buffers_size 64k;              proxy_temp_file_write_size 64k;        } }

``> 域名重写

我们可以在同一个server中绑定域名www.baidu.com和baidu.com两个域名:

server {    listen 80;    server_name baidu.com www.baidu.com;  }

``但是这样对于SEO非常不利,我们需要使用301(rewrite)将一个域名重定向到另一个,比如将baidu.com重定向到www.baidu.com。这里要依赖于正则表达式的分组(使用$1来引用分组)。

server {    listen 80;    server_name baidu.com www.baidu.com;    if($host!= 'www.baidu.com'){      rewrite ^/(.*)$ http://www.baidu.com/$1 permanent;    }}

``> http重写到https

另外一个需求是将两个域名的所有的http请求转发到https上,提高安全级别,同时实现二级域名转向一级域名。

server {        listen 80;        server_name www.baidu.com baidu.com;        rewrite ^(.*)$  https://$host$1 permanent; } server {        listen 443 ssl;        ssl_certificate /etc/nginx/server.crt;        ssl_certificate_key /etc/nginx/server.key;        server_name www.baidu.com baidu.com;        root /data/www;        index index.html index.htm;        if ($host != 'baidu.com') {              rewrite ^/(.*)$ https://baidu.com/$1 permanent;        }        #... }

``