1、 普通的(静态的)http服务器
server{
listen 80;
server_name skyemperor.top ;
#存放前端项目静态页面
location ~ ^/front_project/.+$ {
root /data/static/;
index index.html index.htm;
expires 2h;
}
location ~ ^/file/.+$ {
root /data/static/;
expires 2h;
}
}
server{
listen 80;
server_name blog.skyemperor.top;
location / {
proxy_pass http://localhost:233;
proxy_set_header Host $host:$server_port;
}
}
这样如果访问http://localhost 就会默认访问到root目录下面的index.html,如果一个网站只是静态页面的话,那么就可以通过这种方式来实现部署。
server {
listen 80;
server_name localhost;
location / {
root /root; //思路:通过/将所有的请求,转发给root处理
index index.html;
}
}
2、反向代理
访问localhost的时候,就相当于访问localhost:8080了
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port; //思路:通过/,将所有的请求,转发给第3方处理
}
}
3、Redirect(重定向)语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ "^star\.igrow\.cn$" {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
4、防盗链
location ~* \.(gif|jpg|png|bmp)$ {
valid_referers none blocked *.skyemperor.com server_names ~\.google\. ~\.baidu\.;
if ($invalid_referer) {
return 403;
#rewrite ^/ http://www.ttlsa.com/403.jpg;
}
}
5、根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) { //只能是文件,因为这用-f判断了
expires 1h;
break;
}
}
6、设置图片缓存(过期)时间
location ~* \.(jpg|jpeg|png)$ {
expires 5d;
}
7、禁止访问某个目录
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test; #所有用户都禁止访问这个目录
deny all;
}
8、动静分离
server {
listen 80;
server_name localhost;
location / {
root /root;
index index.html;
}
# 所有静态请求都由nginx处理,存放目录为html
location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
root /root;
}
# 所有动态请求都转发给tomcat处理
location ~ .(jsp|do)$ {
proxy_pass http://test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root e:wwwroot;
}
}
安全机制
消除目录浏览漏洞
autoindex off
隐藏版本号
server_tokens off;
配置ssl
ssl_certificate
ssl_certificate_key
禁用所有不需要的 HTTP 方法
location / {
limit_except GET HEAD POST { deny all; }
}
Comments | 0 条评论