CentOS6.5 yum安装配置nginx 以及相关配置
来源:互联网 发布:一些计算机算法的例子 编辑:程序博客网 时间:2024/06/05 21:53
wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
chmod +x nginx-release-centos-6-0.el6.ngx.noarch.rpm
rpm -i nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install nginx
iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT
service nginx start
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 102400;
events {
use epoll;
worker_connections 102400;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
charset utf-8;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
access_log off;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
server_tokens off;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css text/javascript application/xml;
gzip_vary on;
upstream web_app {
server 127.0.0.1:8785;
server 127.0.0.1:8786;
server 120.25.148.194:8786; #这里会转到另外一台服务器上tomcat上
}
upstream web_apps {
server 123.57.223.31:8743;
server 123.57.223.31:8744;
server 120.25.148.194:443; #这里会转到另外一台服务器上的nginx+tomcat上
}
server {
listen 80;
server_name www.lesongliwu.com;
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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_pass http://web_app;
# expires 1h;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /data/lesongliwu;
#expires defined client broser cache time
expires 1d;
}
}
server {
listen 443 ssl;
server_name www.lesongliwu.com;
ssl_certificate /etc/nginx/1_www.lesongliwu.com_bundle.crt;
ssl_certificate_key /etc/nginx/2_www.lesongliwu.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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_pass https://web_apps;
# expires 1h;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /data/lesongliwu;
#expires defined client broser cache time
expires 1d;
}
location /NginxStatus
{
stub_status on;
# access_log off;
auth_basic "NginxStatus";
}
}
include /etc/nginx/conf.d/*.conf;
}
Nginx Location配置总结
语法规则: location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~*
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
例子,有如下匹配规则:
location = / {
}
location = /login {
}
location ^~ /static/ {
}
location ~ \.(gif|jpg|png|js|css)$ {
}
location ~* \.png$ {
}
location !~ \.xhtml$ {
}
location !~* \.xhtml$ {
}
location / {
}
那么产生的效果如下:
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。
所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
}
三、ReWrite语法
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
2、下面是可以用作判断的全局变量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
四、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;
}
}
五、防盗链
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
六、根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
七、禁止访问某个目录
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
一些可用的全局变量:
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
Nginx禁止恶意User Agent
禁用一些User Agent可以节省一些流量也可以防止一些恶意的访问,尤其是部份搜索引擎爬虫,例如我们的网站就是一个地方性站点,没有必要被一些国外的搜索引擎爬虫索引,都可以禁掉,具体操作如下
1、编辑该文件:
# vi /usr/local/nginx/conf/nginx.conf
2、在Server段内增加以下内容(示例):
#禁止Scrapy等工具的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
return 403;
}
#禁止指定UA及UA为空的访问
if ($http_user_agent ~ "FeedDemon|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$" ) {
return 403;
}
#禁止非GET|HEAD|POST方式的抓取
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 403;
}
有一些单词中间有空格,所以两边需要使用双引号,禁用了若干个搜索引擎爬虫,还有几个恶意灌水机等,可以分析日志根据情况屏蔽恶意的User Agent。
- CentOS6.5 yum安装配置nginx 以及相关配置
- CentOS6.5 yum安装svn以及配置
- Centos6.5服务器配置nginx的https证书(yum 安装的nginx)
- CentOS6.5 yum安装nginx
- centos6.5直接yum安装nginx,并且支持php访问的配置
- centos6.5直接yum安装nginx,并且支持php访问的配置
- centOS6.5下安装Nginx配置
- centos6 yum 安装nginx
- yum安装的nginx配置
- Centos6.5服务器环境搭建之安装Nginx以及Https访问方式的配置
- centos6.4yum安装和配置zabbix_server
- Nginx 安装以及配置
- 卸载redhat6.5的yum,安装centos6的yum,配置第三房yum源
- CentOS6.2 yum安装配置lnmp服务器(Nginx+PHP+MySQL)
- CentOS6.2 yum安装配置LNMP服务器(Nginx+PHP+MySQL)
- centos6.6直接yum安装nginx,并且支持php访问的配置
- 配置CentOS6 yum源
- 检测MySQL PHP nginx是否安装成功 以及相关配置
- NVARCHAR 和VARCHAR区别和使用
- java concurrent
- UILabel补充
- 应聘时给HR最漂亮的回答
- Logistic回归(实例)
- CentOS6.5 yum安装配置nginx 以及相关配置
- 第三周上机实践项目 项目1--个人所得税计算器
- PHP表单处理篇
- 关于github for windows 登录失败提示密码账户错误的解决方案
- 【转】unity3d所要知道的基础知识体系大纲
- 十字链表(Java)
- android 外部启动activity,自定义action,action常量大全
- 【Java】容器类学习之路(二)Collection详解:List、Set和Queue
- JS与OC交互(iPhone项目)中JavaScriptCore.framework的使用