using nginx as http load balancer

Introduction

Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.

It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.

Load balancing methods

The following load balancing mechanisms (or methods) are supported in nginx:

  • round-robin — requests to the application servers are distributed in a round-robin fashion,
  • least-connected — next request is assigned to the server with the least number of active connections,
  • ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address).

Default load balancing configuration

The simplest configuration for load balancing with nginx may look like the following:

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

In the example above, there are 3 instances of the same application running on srv1-srv3. When the load balancing method is not specifically configured, it defaults to round-robin. All requests are proxied to the server group myapp1, and nginx applies HTTP load balancing to distribute the requests. Continue reading “using nginx as http load balancer”

sql server 分区表

IF EXISTS(
       SELECT 1
       FROM   sys.databases AS d
       WHERE  d.name = 'Test_1'
   )
    DROP DATABASE Test_1
 GO
 CREATE DATABASE [Test_1] ON PRIMARY(
                                        NAME
                                        =
                                        N'Test_1',
                                        FILENAME
                                        =
                                        N'E:\Database\Sharding\Test_1.mdf',
                                        SIZE
                                        =
                                        10240KB,
                                        MAXSIZE
                                        =
                                        UNLIMITED,
                                        FILEGROWTH
                                        =
                                        1024Kb
                                    ),
 FILEGROUP [Test_A](
                       NAME = N'Test_A',
                       FILENAME = 'E:\Database\Sharding\Test_A.ndf',
                       SIZE = 10240kb,
                       MAXSIZE = UNLIMITED,
                       FILEGROWTH = 1024kB
                   ),
 FILEGROUP [Test_B](
                       NAME = N'Test_B',
                       FILENAME = 'E:\Database\Sharding\Test_B.ndf',
                       SIZE = 10240kb,
                       MAXSIZE = UNLIMITED,
                       FILEGROWTH = 1024kB
                   )
 LOG ON (
            NAME = N'Test_log',
            FILENAME = 'E:\Database\Sharding\Test_Log.ldf',
            SIZE = 10240kb,
            MAXSIZE = UNLIMITED,
            FILEGROWTH = 1024kB
        ) COLLATE Chinese_PRC_CI_AS

GO
 USE [Test_1]

IF EXISTS(
       SELECT 1
       FROM   sys.partition_functions AS pf
       WHERE  pf.name = 'TEST_PART'
   )
    DROP PARTITION FUNCTION [TEST_PART]
 GO
 -- set a partiton condition
 CREATE PARTITION FUNCTION TEST_PART(INT) AS RANGE LEFT FOR VALUES(2)
 GO
IF EXISTS(
       SELECT 1
       FROM   sys.partition_schemes AS ps
       WHERE  ps.name = 'TEST_SCH'
   )
    DROP PARTITION SCHEME [TEST_SCH]
 GO
 --set a scheme for table using.
 CREATE PARTITION SCHEME TEST_SCH
 AS PARTITION [TEST_PART] TO (Test_A, Test_B)
 GO
IF OBJECT_ID('STUDENT', 'U') IS NOT NULL
    DROP TABLE STUDENT
 GO
CREATE TABLE STUDENT
(
	Id        INT IDENTITY(1, 1) NOT NULL,
	NAME      VARCHAR(10) NULL,
	CLASS     INT NULL,
	GRADE     INT NULL
) ON TEST_SCH(Id) -- set scheme
 GO
INSERT INTO STUDENT
  (
    -- Id -- this column value is auto-generated
    NAME,
    CLASS,
    GRADE
  )
VALUES
  (
    'A',
    1,
    1
  );
INSERT INTO STUDENT
  (
    -- Id -- this column value is auto-generated
    NAME,
    CLASS,
    GRADE
  )
VALUES
  (
    'B',
    2,
    2
  );
INSERT INTO STUDENT
  (
    -- Id -- this column value is auto-generated
    NAME,
    CLASS,
    GRADE
  )
VALUES
  (
    'C',
    3,
    3
  );
INSERT INTO STUDENT
  (
    -- Id -- this column value is auto-generated
    NAME,
    CLASS,
    GRADE
  )
VALUES
  (
    'D',
    4,
    4
  )
 GO

using more javascript syntax inside of angular directive template

在angularjs中,view中避免不了会用一些javascript,你会发现很多标准的javascript语法在html template中无法执行,从而达不到你预期的结果,比如:{{typeof(“A”)}} ,预期输出字符串string;  {{angular.isString(“A”)}},预期输出字符串true,这是因为html template是交由angularjs负责解释和编译的,而在template的对应作用域中,并未去支持所有的标准javascript语法和对象,其中就包括typeof(),但是支持比如”==”,”!==”等一些常用语法。

但是如果想要template支持更多的javascript对象和方法的话,那么就要在template的对应controller的scope中添加引用,比如:在controller中添加scope.angular=angular。那么在template中就可以使用angular.isString(“A”)了。

Sendmail Settings

通常新安装的Linux主机,hostname我们不会将其设置成真实的FQDN形式,这会造成在后面做一些应用服务器时,产生一些影响,比如mail server,做为mail server,如果你的当前hostname是linux.local,而你希望以[email protected]发送邮件出去,那么在没有进行其它配置的情况下,当你将邮件发送后,接收人那里会显示[email protected],并由[email protected]代发。有一些邮件接收服务器就会将你这封邮件做为垃圾邮件,放在垃圾箱里,那么该怎么更改呢,以做到只显示[email protected]呢,下面是sendmail的设置方式:

  1. vim /etc/mail/sendmail.mc
    MASQUERADE_AS(`zhuoyue.me')dnl 
    FEATURE(`masquerade_envelope')dnl 
    FEATURE(`masquerade_entire_domain')dnl 
    MASQUERADE_DOMAIN(`zhuoyue.me')dnl
  2. make  -f /etc/mail/Makefile
  3. service sendmail reload

Application Pool Identities

Introduction

Whether you are running your site on your own server or in the cloud, security must be at the top of your priority list. If so, you will be happy to hear that IIS has a security feature called the application pool identity. This feature was introduced in Service Pack 2 (SP2) of Windows Server 2008 and Windows Vista. An application pool identity allows you to run an application pool under a unique account without having to create and manage domain or local accounts. The name of the application pool account corresponds to the name of the application pool. The image below shows an IIS worker process (W3wp.exe) running as the DefaultAppPool identity.

Application Pool Identity Accounts

Worker processes in IIS 6.0 and in IIS 7 run as Network Service by default. Network Service is a built-in Windows identity. It doesn’t require a password and has only user privileges; that is, it is relatively low-privileged. Running as a low-privileged account is a good security practice because then a software bug can’t be used by a malicious user to take over the whole system.

However, a problem arose over time as more and more Windows system services started to run as Network Service. This is because services running as Network Service can tamper with other services that run under the same identity. Because IIS worker processes run third-party code by default (Classic ASP, ASP.NET, PHP code), it was time to isolate IIS worker processes from other Windows system services and run IIS worker processes under unique identities. The Windows operating system provides a feature called “virtual accounts” that allows IIS to create a unique identity for each of its application pools. Click here for more information about Virtual Accounts. Continue reading “Application Pool Identities”

李雷和韩梅梅身世之谜揭晓

李雷和韩梅梅,他俩暗恋没有?交往没有?分手没有?在似乎有着漫长时光的青春岁月,每个曾捧着初中英语书的同学,白日无聊,也许都做过这样那样天马行空的想象。据统计,从1990年到2000年,写有“李雷和韩梅梅”故事的初中英语教科书,教育了整整一亿人。

官方权威解释,李雷和韩梅梅身世之谜揭晓

那么,抛开现在各种以李雷和韩梅梅为原型的歌曲、话剧、漫画、微电影、改编故事不提,你想不想听听李雷和韩梅梅的官方“八卦”?聊聊这套传奇教材背后的故事?

机会来了。日前,在“中国百年教科书整理与研究”项目成果发布,微信公众号“人民日报政文”的小编亲自探查,揭开了李雷和韩梅梅的“身世之谜”。

“划时代”的教科书 Continue reading “李雷和韩梅梅身世之谜揭晓”

Windows PowerShell Quick Reference

How to Access Arguments

To access command-line arguments used when starting a script use the automatic variable $args. You can cycle through the individual arguments in the $args collection by using code similar to this:

foreach ($i in $args) {$i}

To access a particular argument use the collection index number, with 0 representing the first item in the collection, 1 representing the second item, etc:

$args[0]

You can reference the last item in a collection by using the index number –1:

$args[-1]

How to Use Colored Text

To display text in a different color use the Write-Host cmdlet and specify a foreground color:

How to Get More Information

You can also specify a different background color: Continue reading “Windows PowerShell Quick Reference”

Nginx virtual host & php-fpm settings

共享我的当前VPS中关于nginx的一些设置,防止自己忘记了。

针对Nginx global的设置(没有写的,说明使用了默认的设置了):

#Nginx进程的用户名和用户组设置,最小权限原则
user www-data www-data;
#Nginx初始化工作进程数
worker_processes 4; 
#启用Nginx的核心安全策略,比如SQL注入,跨站之类
include /etc/nginx/naxsi_core.rules; 
#启用gzip压缩输出,对IE6禁用gzip
gzip on; 
gzip_disable "msie6";

针对Virtaul Host的设置:

首先看Reserve Proxy,

server { 
 ### server port and name ### 
 listen 443; 
 ssl on; 
 server_name ******.zhuoyue.me 
 ### SSL log files ### 
 access_log /var/log/nginx/ssl-access.log; 
 error_log /var/log/nginx/ssl-error.log; 
 
 ### SSL cert files ### 
 ssl_certificate /home/niyouzhu/nginxssl/server.crt; 
 ssl_certificate_key /home/niyouzhu/nginxssl/server.key; 
 
 ### Add SSL specific settings here ### 
 
 
 ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; 
 ssl_ciphers RC4:HIGH:!aNULL:!MD5; 
 ssl_prefer_server_ciphers on; 
 keepalive_timeout 60; 
 ssl_session_cache shared:SSL:10m; 
 ssl_session_timeout 10m; 
 
 ### We want full access to SSL via backend ### 
 location / {
 proxy_pass https://localhost:4200; 
 
 ### force timeouts if one of backend is died ## 
 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; 
 
 ### Set headers #### 
 proxy_set_header Accept-Encoding ""; 
 proxy_set_header Host $host; 
 proxy_set_header X-Real-IP $remote_addr; 
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
 
 ### Most PHP, Python, Rails, Java App can use this header ### 
 #proxy_set_header X-Forwarded-Proto https;## 
 proxy_set_header X-Forwarded-Proto $scheme; 
 add_header Front-End-Https on; 
 proxy_redirect off; 
 }
 }

上面关于SSL自签名证书的创建,可以看我的另一篇文章:Ubuntu+LAMP+Nginx

再来看一般的Virtual Host设置, Continue reading “Nginx virtual host & php-fpm settings”

20年后的今天,世界首个转基因动物开始供人类食用

在美国,转基因作物最早问世于1996年,随后迅速被广泛接受。2013年,从种植面积来看,美国至少95%的甜菜,93%的大豆,90%的棉花和玉米均为转基因品种,这一数据来自美国农业部国家农业统计局。

2015年11月19日,经过全面的科学论证,美国食品与药品监督管理局(FDA)批准了世界上第一种食用转基因动物——AquAdvantage转基因大西洋鲑鱼(Atlantic salmon),俗称三文鱼。这种转基因三文鱼生长速度约为普通三文鱼的两倍,可节省75%的饲料成本。

转基因三文鱼最早于问世1989年,来自于美国加州大学伯克利分校开发的抗冻蛋白(antifreeze protein)技术。A / F Protein公司最初成立于1991年。1993年该公司向FDA寻求监管指导,1995年开始申请商业化。

1996年,A/F Protein公司从多伦多大学(University of Toronto)和纽芬兰纪念大学(Memorial University of Newfoundland)获得了AquAdvantage技术许可,随后于2000年重组成为两个独立的实体公司:A/F Protein公司,保留了抗冻蛋白技术;AquaBounty Farm公司,获得了AquAdvantage技术。AquaBounty Farm公司的总部设在马萨诸塞州沃尔瑟姆2004年更名为AquaBounty Technologies。 Continue reading “20年后的今天,世界首个转基因动物开始供人类食用”