nginx 配置小结

#用户

user  webapp;

#工作进程数量

worker_processes  8;

#平均分配到CPU(当前是8核)

worker_cpu_affinity 10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001;

#worker_cpu_affinity auto;

#错误日志记录

error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        logs/nginx.pid;

worker_rlimit_nofile 10240;

#事件类型

events {

use epoll;

#每个进程的最大连接数

worker_connections  15000;

multi_accept on;

}

http {

include       mime.types;

default_type  application/octet-stream;

# 修改默认服务器类型标签,避免某些安全隐患

server_tokens off;

server_tag ‘NULL‘;

#定义日志格式

#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  logs/access.log  main;

#设定请求缓冲

client_header_buffer_size    32k;

large_client_header_buffers 4 4k;

sendfile        on;

tcp_nopush     on;

tcp_nodelay     on;

#keepalive_timeout  0;

keepalive_timeout  65;

server_tokens off;

open_file_cache     max=65535       inactive=20s;

open_file_cache_valid       30s;

open_file_cache_min_uses    1;

#开启gzip模块

gzip  on;

gzip_min_length  1000;

gzip_buffers     4 16k;

gzip_comp_level 4;

gzip_types  text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

gzip_vary   on;

gzip_disable        "MSIE [1-6]\.";

fastcgi_intercept_errors on;

#定义负载均衡后端服务器

upstream TOMCAT {

server 192.168.1.1:8081 max_fails=1;

server 192.168.1.1:8082 max_fails=1;

server 192.168.1.1:8083 max_fails=1;

server 192.168.1.1:8084 max_fails=1;

ip_hash;

#负载均衡健康检查  需要安装nginx_upstream_check_module

check interval=3000 rise=2 fall=5 timeout=1000  type=http;

#check_http_send "GET  / HTTP/1.0\r\n\r\n";

#check_http_expect_alive http_2xx http_3xx;

}

#limit模块限制

limit_req_zone  $binary_remote_addr  zone=zone_name:20m    rate=5r/s;

# 服务器定义

server {

listen       8080;

#定义多个端口

#listen     9090;

server_name  192.168.1.1 localhost;

set $addr $remote_addr;

proxy_store off;

error_page 404   /404.html;

charset  utf-8;

#设定上传文件的最大尺寸

client_max_body_size    20m;

#定义时间参数,用作日志生成

if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {

set $year $1;

set $month $2;

set $day $3;

}

#日志,指定目录及日期,可自动按天生成

access_log  /nfs/nginxlog/nginx-$year-$month-$day-access.log;

# 指定HTTP访问方式:非GET或者POST方式,返回403;一般为Web、WAP使用

if ($request_method !~ ^(GET|POST)$) {

return 403;

}

#判断客户端地址

if ($remote_Addr != ‘4.4.4.4‘ ){

return 403;

}

if ($remote_Addr = ‘2.2.2.2‘ ) || ($remote_Addr = ‘3.3.3.3‘ ) {

........;

}else{

return 403;

}

#可以根据判断结果设定参数值,再根据参数值确定具体操作

if ($request_uri ~ \.(conf|xml)$) {

set $seo 2;

}

if ($seo = 2) {

proxy_pass http://192.168.1.2:8080;

rewrite ^.*$ /api/1$request_uri;

break;

}

#判断用户访问的客户端类型

if ( $http_user_agent ~* "(Android|iPhone|Windows Phone|UC|Kindle)" ){

rewrite ^(.*) http://www.xxx.cn/yy/ permanent;

}

#设置参数初始值,并在进行判断后赋值

set $SESSIONID "-";

if ( $http_cookie ~* "WAP_SESSIONID=([^;]+)(?:;|$)" ){

set $SESSIONID $1;

}

#匹配用户访问url路径,然后进行相应处理

location =/ {

return 404;

}

location ~ ^/(WEB-INF)/ {

deny all;

}

location  ^~  class {

# 匹配任何已 images 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。

deny all;

}

#匹配用户访问的文件类型,符合即指向指定目录

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|htc|ico)$ {

root /webapp/web/image;

access_log off;

expires      30d;

}

#重定向

location  /content {

rewrite  ^/(.*)$    /lcpproxy/content.php?$1 last;

}

#使用LUA脚本

location /queryCache {

default_type ‘text/plain‘;

content_by_lua_file  /home/fjccwt/nginx/lua/queryCache.lua;

}

#使用python脚本   需要ngx_python模块

location /content_by_python_file {

content_by_python_file /webapp/openresty/hello.py;

}

#反向代理

location /test/ {

limit_req  zone=zone_name   burst=1500  nodelay;

proxy_pass      http://tomcat/test/;

proxy_redirect          off;

proxy_set_header        Host $host:$server_port;

proxy_set_header        X-Real-IP $remote_addr;

proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

client_max_body_size    20m;

client_body_buffer_size 500k;

proxy_max_temp_file_size 0;

proxy_connect_timeout   60;

proxy_send_timeout      60;

proxy_read_timeout      60;

proxy_buffer_size       128k;

proxy_buffers           8 128k;

proxy_busy_buffers_size 256k;

proxy_temp_file_write_size 1024k;

}

#nginx管理配置

location /ngx_status {

stub_status on;

access_log off;

#设定NGINX管理平台访问密码

#auth_basic              "NginxStatus";

#auth_basic_user_file    htpasswd;

allow 127.0.0.1;

deny all;

}

}

#HTTPS配置

server {

listen       8443;

server_name  localhost;

ssl                  on;

ssl_certificate      /webapp/softs/sslkey/server.crt;

ssl_certificate_key  /webapp/softs/sslkey/server.key;

ssl_session_timeout  5m;

ssl_protocols  SSLv2 TLSv1;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers   on;

if ($request_method !~ ^(GET|HEAD|POST)$ ) {

return 403;

}

location / {

root   html;

index  index.html index.htm;

}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

#    listen       8000;

#    listen       somename:8080;

#    server_name  somename  alias  another.alias;

#    location / {

#        root   html;

#        index  index.html index.htm;

#    }

#}

# HTTPS server

#

#server {

#    listen       443 ssl;

#    server_name  localhost;

#    ssl_certificate      cert.pem;

#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;

#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;

#    ssl_prefer_server_ciphers  on;

#    location / {

#        root   html;

#        index  index.html index.htm;

#    }

#}

}

时间: 2024-08-10 21:28:37

nginx 配置小结的相关文章

nginx负载均衡+keepalived高可用完全配置小结

nginx做负载均衡(无高可用) 大致步骤. 1. 前端 nginx安装,pcre安装,具体步骤不解释. 2. 负载配置 A. 默认轮循 在nginx.conf  里加入一行 include upstream.conf,然后所有的负载均衡的配置直接在upstream.conf里配置. [[email protected] conf]# cat upstream.conf upstream httpservers { server 192.168.137.10:80 weight=5; serve

Nginx 配置

Nginx 配置 不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦.不过,我们往往只是需要一个静态 Server,或者一个反向代理 Server,这对 Nginx 来说小菜一碟. 本文将给大家介绍 Nginx 配置的基本知识,不想细看的同学可以直接跳到最后一个例子. 简介 Nginx 的安装就不解释了,方便起见,建议在各平台可以直接执行对应安装命令: # CentOS

[转帖]nginx配置ssl加密(单/双向认证、部分https)

nginx配置ssl加密(单/双向认证.部分https) https://segmentfault.com/a/1190000002866627 nginx下配置ssl本来是很简单的,无论是去认证中心买SSL安全证书还是自签署证书,但最近公司OA的一个需求,得以有个机会实际折腾一番.一开始采用的是全站加密,所有访问http:80的请求强制转换(rewrite)到https,后来自动化测试结果说响应速度太慢,https比http慢慢30倍,心想怎么可能,鬼知道他们怎么测的.所以就试了一下部分页面h

linux学习笔记——搭建基于nginx的web服务器、多核配置、nginx配置参数

############ 认识nginx #############Nginx:(发音同 engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用.  其优点是轻量级(占有内存少),高并发(并发能力强),事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用ngi

12.17 Nginx负载均衡;12.18 ssl原理;12.19 生产ssl密钥对;12.20 Nginx配置ssl

扩展: 针对请求的uri来代理 http://ask.apelearn.com/question/1049 根据访问的目录来区分后端web http://ask.apelearn.com/question/920 12.17 Nginx负载均衡 1. 安装dig命令: [[email protected] ~]# yum install -y bind-utils 2. 用dig获取qq.com的ip地址: [[email protected] ~]# dig qq.com 3. 创建ld.co

Nginx配置详解

Nginx配置文件主要分成四部分: main(全局设置)指令将影响其它所有部分的设置: server(主机设置)指令主要用于指定虚拟主机域名.IP和端口: upstream(上游服务器设置,主要为反向代理.负载均衡相关配置)指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡: location(URL匹配特定位置后的设置),每部分包含若干个指令.location部分用于匹配网页位置(比如,根目录"/","/images",等等). 他们之间的关系式:

LNMP架构应用实战——Nginx配置虚拟主机

LNMP架构应用实战--Nginx配置虚拟主机        前面介绍了nginx服务的安装与配置文件,今天介绍下它的另一种实用配置--"虚拟主机",每个虚拟主机可以是一个独立的网站,可以具有独立的域名,同一台服务器上的不同的虚拟主机之间是独立的,用户访问不同虚拟主机如同访问不同的服务器一样,因此它不需要为一个单独的WEB站点提供单独一个nginx服务器和一个单独的nginx进程 1.nginx虚拟主机简单介绍 同apache服务一样,它也有三种不同的虚拟主机,基于域名的虚拟主机.基于

nginx 配置详解

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用.其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:京东.新浪.网易.腾讯.淘宝等. 一.event模型 传统的基于进程和线程的模型在处理并发连接的时候针对每个连接

劳动节配置F5负载均衡配置小结

2014年5月1日是劳动节,是劳动人民应该休息的节日,呵呵结果这几天却是作死的节奏,天天加班到2点半,真实很辛苦呀,整个过程很艰辛但是结果是好的. 1.配置LC1600链路负载均衡,首先要激活license的.在system-license-Re activacte 2.要创建VLAN,由于是联通线路.电信线路和下行线路,就创建vlan_cnc vlan_ctc vlan1,每个vlan要设置对应的端口,比如vlan_cnc对应光纤口2.1 3.创建完vlan 要写selfip即每个vlan的地