WenJunjie Blog

sysadmin

Disk Speed Test

| Comments

conv=fsync 在dd命令结束前同步数据和元数据(metaData)

conv=fdatasync 在dd命令结束前只同步数据

oflag=direct 使用DirectIO

oflag=sync 使用同步I/O,每次写都要物理写入磁盘

oflag=nonblock 非阻塞I/O, 可以调用不会永远阻塞的 I/O 操作

direct (use direct I/O for data) dsync (use synchronized I/O for data) sync (likewise, but also for metadata)

test command dd bs=1M count=256 if=/dev/zero of=test conv=fdatasync dd bs=1M count=256 if=/dev/zero of=test oflag=dsync dd bs=1M count=256 if=/dev/zero of=test oflag=direct dd bs=1M count=256 if=/dev/zero of=test oflag=sync dd bs=1M count=256 if=/dev/zero of=test oflag=nonblock

Linode CentOS release 6.2 (Final) [root@Linode-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test conv=fdatasync 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 0.926112 s, 290 MB/s [root@Linode-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=dsync 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 0.746619 s, 360 MB/s [root@Linode-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=direct 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 0.296677 s, 905 MB/s [root@Linode-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=sync 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 0.742054 s, 362 MB/s [root@Linode-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=nonblock 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 0.484081 s, 555 MB/s

Linode SSD CentOS release 6.5 (Final) [root@Linode-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test conv=fdatasync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,0.467751 秒,574 MB/秒 [root@Linode-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=dsync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,0.574327 秒,467 MB/秒 [root@Linode-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=direct 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,0.29828 秒,900 MB/秒 [root@Linode-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=sync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,0.551949 秒,486 MB/秒 [root@Linode-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=nonblock 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,0.421991 秒,636 MB/秒

Digital SSD CentOS release 6.4 (Final) [root@DigitalOcean-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test conv=fdatasync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,1.17245 秒,229 MB/秒 [root@DigitalOcean-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=dsync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,1.84996 秒,145 MB/秒 [root@DigitalOcean-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=direct 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,1.07656 秒,249 MB/秒 [root@DigitalOcean-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=sync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,1.70525 秒,157 MB/秒 [root@DigitalOcean-SSD ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=nonblock 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,0.807409 秒,332 MB/秒

Aliyun CentOS release 6.4 (Final) [root@Aliyun-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test conv=fdatasync 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 6.09344 s, 44.1 MB/s [root@Aliyun-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=dsync 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 6.1134 s, 43.9 MB/s [root@Aliyun-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=direct 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 9.23089 s, 29.1 MB/s [root@Aliyun-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=sync 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 7.92477 s, 33.9 MB/s [root@Aliyun-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=nonblock 256+0 records in 256+0 records out 268435456 bytes (268 MB) copied, 4.94173 s, 54.3 MB/s

GorillaServers CentOS release 6.5 (Final) [root@GorillaServers-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test conv=fdatasync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,2.47875 秒,108 MB/秒 [root@GorillaServers-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=dsync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,18.6242 秒,14.4 MB/秒 [root@GorillaServers-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=direct 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,2.69742 秒,99.5 MB/秒 [root@GorillaServers-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=sync 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,21.6532 秒,12.4 MB/秒 [root@GorillaServers-Disk ~]# dd bs=1M count=256 if=/dev/zero of=test oflag=nonblock 记录了256+0 的读入 记录了256+0 的写出 268435456字节(268 MB)已复制,2.32105 秒,116 MB/秒

Linux I/O Performance Tests using dd

smartctl -H /dev/sda smartctl -H /dev/sdb

Install Cp Diff

| Comments

  1. 如果目标文件存在,cp会先清空文件后再写入新文件,而install则会先删除原先的文件然后再写入新文件; 对正在使用的文件写入内容可能会导致一些问题,写入正在执行的文件可能会失败或已经在持续写入的文件句柄中写入新文件会产生错误文件。
  2. install可处理文件权限问题;
  3. install可打印更多debug信息及自动处理selinux上下文件问题
  4. cp要负载较高的服务器,复制大文件时可能会coredump

install主要用法如下: install [OPTION]… SOURCE… DIRECTORY 此时,DIRECTORY必须存在,否则被当成新的文件 install [OPTION]… -t DIRECTORY SOURCE… install [OPTION]… -d DIRECTORY… 如果目录不存在则创建 -b:为每个已存在的目的地文件进行备份; -D:创建目的地前的所有目录,然后将来源复制到目的地 -g:自行设置所属的组; -m:自行设置权限,而不是默认的rwxr-xr-x -o:自行设置所有者 -p:以来源文件的修改时间作为相应的目的地的文件属性

install -d /usr/bin install -p -D -m 0755 targets /usr/bin 相当于 mkdir -p /usr/bin cp targets /usr/bin chmod 755 /usr/bin/targets touch /usr/bin/tagets <—- 更新文件时间戳

Oracle Tips

| Comments

sqlplus sqlplus USERNAME/PASSWORD@HOSTNAME

select * from v$dbfile select * from v$tablespace create tablespace NAME datafile ‘FILENAME.dbf’ size 20M; (Create DB)

create user USERNAME identified by PASSWORD; grant connect to USERNAME; grant resource to USERNAME; conn USERNAME/PASSWOD; create table t1 (c1 int); insert into t1 values(1); commit;

http://www.cnblogs.com/chinafine/articles/1755405.html

Compare Directories via Diff

| Comments

制作补丁

diff -uN oldfile newfile > file.patch diff -Nur srcode srcode_update > srcodechange.patch

发布

patch -Np0 < srcodechange.patch

还原

patch -Rp0 < srcodechange.patch

LC_ALL=C TZ=UTC0

diff -Naur patch -p0 patch -N p0 patch -R p0

cd srcode patch -p1 < ../srcodechange.patch patch -pN < patch.file

find . -name *.rej > reject

快速比较两个目录

diff -c -r service service.bak diff -c -a -b -r service service.bak

diff -r service service.bak diff -r –brief service service.bak

diff -qr service service.bak rsync -vnr service/ service.bak/

-n, –dry-run perform a trial run with no changes made rsync -avn src dst –delete –size-only –ignore-existing

可以用: diff -qr directory1/ directory2/ rsync -vnr directory1/ directory2/ 但是很慢。因为文件也被比较了。给rsync加上“–ignore-existing”的选项要好很多。 如果只是想比较目录结构,可以 ls -R directory1 > directory1.txt ls -R directory2 > directory2.txt diff –side-by-side –suppress-common-lines directory1.txt directory2.txt 如果给ls加上“-1s”的选项(注意:是1,2,3的1,不是l,m,n的l),还可以比较文件的大小。

假如我要比较目录A和目录B,使用下面的命令 diff -c -a -b -x .bak -x .log A B > AB.diff gvim AB.diff 这样就可以方便的查看不同了,目前我的vim的diff配色比较舒适。 解释一下参数的意义 -c 采用content的format输出different -a 强制使用text的模式 -b 忽略空白的不同 -x 过滤掉所比较目录中一些不想比较的文件类型,可以使用其他的pattern 实际上如果需要过滤的文件类型比较多的时候,使用-x这个选项就有点麻烦了,查看了文档之后,diff提供了更加方便的参数过滤文件。 -X excludefile 忽略在excludefile中的文件类型,注意每种文件占一行

diff -ruNa A B diff -uN A B

对比两个文件 先过滤掉重复的行 cat 1.txt |sort |uniq -c|sort -rn|awk ‘{print $2}’ >file1.txt cat 2.txt |sort |uniq -c|sort -rn|awk ‘{print $2}’ >file2.txt cat file1.txt |xargs -l1 -i grep {} file2.txt > tmp.txt

echo -n ‘hello’|md5sum|cut -d ‘ ’ -f1 md5sum log.iso |cut -d ‘ ’ -f1 md5sum log.iso > log.iso.md5 md5sum -c log.iso.md5

diff -qr dirA dirB | grep -v -e ‘DS_Store’ -e ‘Thumbs’ | sort > diffs.txt

比较两个目录大小 diff <(du -sh dir1) <(du -sh dir2)

对比文件内容并删除相同部分 对比已经排过序的文件 comm -23 file1 file2

grep -v -f file2 file1

打印出file1、file2文件,第一列为行数、第二列为两文件内容 |然后按第二列排序|删除重复行(sort -u也可以,但它会保留一个重复项)|在按第一列行数排序|打印出第二列 [root@kinggoo.com test1]# awk ‘{print NR,$0}’ file1 file2 |sort -k2|uniq -u -f 1|sort -k1|awk ‘{print $2}’

awk ‘{print $0}’ file1 file2 |sort|uniq -u

排序去重 sort -u [file] = sort [file] | uniq (去重) uniq 和 comm 命令需要使用已经排序好的内容才有意义。注意文件格式要为unix uniq 最重要参数: 默认(去重) | -d(显重) | -u(删重) comm 功能说明:比较两个已排序的文件

第一:两个文件的交集,并集 前提条件:两个文件不得有重复的行(即两个文件都要去重) 1. 取出两个文件的并集 cat file1 file2 | sort | uniq >file3 2. 取出两个文件的交集 cat file1 file2 | sort | uniq -d >file3 3. 删除交集 cat file1 file2 | sort |uniq -u >file3 第二:两个文件合并 1. 一个在上一个在下 cat file1 files2 >file3 2. 一个在左一个在右 paste file1 file2 >file3 3. cat file1 >> file2 第三:一个文件去掉重复行 1. sort file | uniq 把重复的行合并为一行 2. sort file | uniq -u 把重复的行删除,仅保留非重复的行

join -t: -i -1 3 -2 1 TEACHER1.db TEACHER_HOBBY.db cut -d: -f1-3 TEACHER.db

Linux Details

| Comments

  1. 文件隐藏属性 lsattr chattr [+-=][ASacdistu] FileName +: Add one attribute -: Remove one attribute =: Set to be the only attributes that the files have import/useful option a: 只能追加文件内容,不能修改或删除内容 i: 文件不能被删除、改名、不能创建指向它的链接、不能向文件写内容

  2. 文件的特殊权限suid/sgid/sticky bit suid 以文件所有者的权限运行,不能对目录设置suid chmod u+s FileName chmod 4755 FileName sgid 以文件所属组的权限运行,目录里面的文件会继承目录的属性 chmod g+s FileName chmod 2771 FileName/Dir sticky 不能对文件设置sticky bit 对于该目录下的文件,只有它们的所有者才能删除它们 chmod o+t Dir chmod 1777 Dir

  3. ln option target linkname ln source_file target_file

  4. useradd -r -g GroupName -G Grp1 -G 加入另一组 -r 建立系统账号

  5. gdb gdb Program Program.core gdb -p PID gdb /usr/bin/httpd core (gdb) bt

  6. rsync rsync -avz /local/files -e “ssh -p $portNumber” user@remoteip:/path/to/files/ -n, –dry-run –bwlimit=KBPS –delete –existing –progress -d -u –include –exclude rsync -avzi file1 file2 –max-size -W –remove-source-files

rsync -ahvz

–exclude={/dev/,/proc/,/sys/,/tmp/,/run/,/mnt/} –link-dest=comparison_backup_folder

crontab 0 3 * * * Command

^ ^ ^ ^ ^ ^ | | | | | | Minute Hour Day of Month Month Weekday Shell command

  1. scp
  2. grep grep -rin xxx Files gzip -dc myfile.gz | grep abc -A2 -B2 gzip -9 grep -H xxx file grep -h xxx file

  3. netstat netstat -ntlp

  4. lsof 删除文件,文件句柄没释放 lsof |grep REG|awk ‘{if ($7 > 0) print $7, $9, $10)’|sort -rn | head -n 10 deleted

  5. dig 检查根服务和授权域服务器 DNS劫持检查 dig domain.com +trace dig domain.com @8.8.8.8 dig MX domain.com

  6. rpm rpm -ql fail2ban

  7. ps ps -C nginx -o pid,cmd

  8. find find -regex ‘.back\/def\/123..jpg’ find -regextype posix-egrep -regex ‘.abc\/def\/123..(jpg|png)’ find -regextype posix-egrep -regex ‘.abc\/def\/123..(jpg|png)’ -ctime -24 find -regextype posix-egrep -regex ‘.abc\/def\/123..(jpg|png)’ -ctime -24 -exec cp {} ~/yesterday \; find -regextype posix-egrep -regex ‘.abc\/def\/123..(jpg|png)’ -ctime -24 | while read line; do eog $line &; done; eog=eye of gnome

  9. tail tail -f file.txt | grep something –line-buffered

–build=x86_64-linux-gnu

lokkit –selinux=disabled

  1. tar

tar zcvf tar pczvf

mysql

mysqladmin -uroot -p processlist | awk -F “|” ‘{print $2}’|xargs -n 1 mysqladmin -uroot -p kill mysqladmin -uroot -p processlist|awk -F “|” ‘{if($3 == “wordpress”)print $2}’|xargs -n 1 mysqladmin -uroot -p kill

Pligg Nginx Rewrite

| Comments

Pligg 2 On Nginx Rewrite rules

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
server {
    listen       80;
    server_name  www.example.com;
    root   /usr/share/nginx/htdocs/example.com;
    index index.php  index.html index.htm;

    charset utf-8;

    error_page 404 502 = /404error.php;

    autoindex off;

    if ($uri !~ "(.[a-zA-Z0-9]{1,5}|/)$") {
         rewrite /(.*)([^/])$ http://$http_host/$1$2/ permanent;
    }
    if ($http_host !~ "^www.") {
         rewrite /.* http://www.$http_host$uri permanent;
    }


    location / {

    if (!-e $request_filename){
            ## Re-directing Begin
            # Story and Category URLs
            rewrite ^/(all)/([^/]+)/?$ /story.php?title=$2 last;
            rewrite ^/(all)/?$ index.php last;

            # /recent redirection
            rewrite ^/upcoming/recent/?$ /upcoming permanent;
            rewrite ^/upcoming/recent/category/([^/]+)/?$ /upcoming/category/$1 permanent;
            rewrite ^/upcoming/recent/page/(\d+)/?$ /upcoming/page/$1 permanent;
            rewrite ^/upcoming/recent/category/([^/]+)/page/(\d+)/?$ /upcoming/category/$1/page/$2 permanent;
            rewrite ^/recent/?$ /./ permanent;
            rewrite ^/recent/category/([^/]+)/?$ /category/$1 permanent;
            rewrite ^/recent/page/(\d+)/?$ /page/$1 permanent;
            rewrite ^/recent/category/([^/]+)/page/(\d+)/?$ /category/$1/page/$2 permanent;
            rewrite ^/upcoming/recent/([^/]+)/?$ /upcoming/category/$1 permanent;
            rewrite ^/upcoming/recent/([^/]+)/page/(\d+)/?$ /upcoming/category/$1/page/$2 permanent;
            rewrite ^/recent/([^/]+)/?$ /category/$1 permanent;
            rewrite ^/recent/([^/]+)/page/(\d+)/?$ /category/$1/page/$2 permanent;
            rewrite ^/upcoming/([^/]+)/rss/?$ /rss.php?status=queued&category=$1 last;
            rewrite ^/upcoming/rss/?$ /rss.php?status=queued last;
            rewrite ^/(\d+)/rss/?$ /storyrss.php?id=$1 last;
            rewrite ^/([^/]+)/rss/?$ /rss.php?category=$1 last;
            rewrite ^/user/profile/([^/]+)/rss/?$ /userrss.php?user=$1 last;
            rewrite ^/user/profile/([^/]+)/rss/([a-zA-Z0-9-]+)/?$ /userrss.php?user=$1&status=$2 last;
            rewrite ^/rss/?$ /rss.php last;
            rewrite ^/search/([^/]+)/rss/?$ /rss.php?search=$1 last;
            rewrite ^/group/([^/]+)/rss/?$ /rss.php?status=published&group=$1 last;
            rewrite ^/group/([^/]+)/upcoming/rss/?$ /rss.php?status=queued&group=$1 last;
            rewrite ^/group/([^/]+)/shared/rss/?$ /rss.php?status=shared&group=$1 last;
            rewrite ^/([^/]+)/([^/]+)/rss/?$ /storyrss.php?title=$2 last;
            rewrite ^/rss/([a-zA-Z0-9-]+)/?$ /rss.php?status=$1 last;
            rewrite ^/rss/category/([^/]+)/?$ /rss.php?category=$1 last;
            rewrite ^/rss/category/upcoming/([^/]+)/?$ /rss.php?status=queued&category=$1 last;
            rewrite ^/rss/category/published/([^/]+)/?$ /rss.php?status=published&category=$1 last;
            rewrite ^/rss/category/([^/]+)/queued/?$ /rss.php?status=queued&category=$1 last;
            rewrite ^/rss/category/([^/]+)/published/?$ /rss.php?status=published&category=$1 last;
            rewrite ^/rss/category/([^/]+)/group/([^/]+)/?$ /rss.php?category=$1&group=$2 last;
            rewrite ^/rss/category/upcoming/([^/]+)/([^/]+)/?$ /rss.php?status=queued&category=$1&group=$2 last;
            rewrite ^/rss/category/published/([^/]+)/([^/]+)/?$ /rss.php?status=published&category=$1&group=$2 last;
            rewrite ^/rss/search/([^/]+)/?$ /rss.php?search=$1 last;
            rewrite ^/rssfeeds/?$ /rssfeeds.php last;

            ## URL Method 2 Begin ##

            rewrite ^/?$ /index.php last;
            rewrite ^/advanced-search/?$ /advancedsearch.php last;
            rewrite ^/category/([^/]+)/?$ /index.php?category=$1 last;
            rewrite ^/category/([^/]+)/([^/]+)/?$ story.php?title=$2 last;
            rewrite ^/login/?$ /login.php last;
            rewrite ^/login/([a-zA-Z0-9-]+)/?$ /login.php?return=$1 last;
            rewrite ^/login/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /login.php?return=$1/$2 last;
            rewrite ^/logout/?$ /login.php?op=logout&return=index.php last;
            rewrite ^/logout/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /login.php?op=logout&return=$1/$2 last;
            rewrite ^/out/([\d]+)/?$ /out.php?id=$1 last;
            rewrite ^/out/(https?:.+)$ /out.php?url=$1 last;
            rewrite ^/out/([^/]+)/?$ /out.php?title=$1 last;
            rewrite ^/profile/?$ /profile.php last;
            rewrite ^/register/?$ /register.php last;
            rewrite ^/search/([^/]+)/page/(\d+)/?$ /search.php?search=$1&page=$2 last;
            rewrite ^/search/(.+)/?$ /search.php?search=$1 last;
            rewrite ^/searchurl/(.+)/?$ /search.php?url=$1 last;
            rewrite ^/settemplate/?$ /settemplate.php last;
            rewrite ^/story/([0-9]+)/?$ /story.php?id=$1 last;
            rewrite ^/story/([^/]+)/?$ /story.php?title=$1 last;
            rewrite ^/story/([0-9]+)/editcomment/([0-9]+)/?$ /edit.php?id=$1&commentid=$2 last;
            rewrite ^/story/([0-9]+)/edit/?$ /editlink.php?id=$1 last;
            rewrite ^/submit/?$ /submit.php last;
            rewrite ^/tag/([^/]+)/([^/]+)/?$ /search.php?search=$1&tag=true&from=$2 last;
            rewrite ^/tag/([^/]+)/?$ /search.php?search=$1&tag=true last;
            rewrite ^/tag/([^/]+)/page/(.+)/?$ /search.php?search=$1&tag=true&page=$2 last;
            rewrite ^/tagcloud/?$ /cloud.php last;
            rewrite ^/tagcloud/range/([0-9]+)/?$ /cloud.php?range=$1 last;
            rewrite ^/topusers/?$ /topusers.php last;
            rewrite ^/trackback/([0-9]+)/?$ /trackback.php?id=$1 last;
            rewrite ^/upcoming/?$ /upcoming.php last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/?$ /upcoming.php?part=$1 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/category/([^/]+)/?$ /upcoming.php?part=$1&category=$2 last;
            rewrite ^/upcoming/([^/]+)/?$ /upcoming.php?category=$1 last;
            rewrite ^/upcoming/page/([0-9]+)/?$ /upcoming.php?page=$1 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/page/(\d+)/?$ /upcoming.php?part=$1&page=$2 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/category/([^/]+)/page/(\d+)/?$ /upcoming.php?part=$1&category=$2&page=$3 last;
            rewrite ^/user/?$ /user.php last;
            rewrite ^/user/search/([^/]+)/?$ /user.php?view=search&keyword=$1 last;
            rewrite ^/user/profile/([^/]+)/following/?$ /user.php?login=$1&view=following last;
            rewrite ^/user/profile/([^/]+)/followers/?$ /user.php?login=$1&view=followers last;
            rewrite ^/user/([^/]+)/?$ /user.php?view=$1 last;
            rewrite ^/user/([^/]+)/([^/]+)/?$ /user.php?view=$1&login=$2 last;
            rewrite ^/user/([^/]+)/link/([0-9+]+)/?$ /user_add_remove_links.php?action=$1&link=$2 last;

            ## Admin
            rewrite ^/admin/?$ /admin/admin_index.php last;
            rewrite ^/admin_comments/page/([^/]+)/?$ /admin/admin_comments.php?page=$1 last;
            rewrite ^/admin_links/page/([^/]+)/?$ /admin/admin_links.php?page=$1 last;
            rewrite ^/admin_users/page/([^/]+)/?$ /admin/admin_users.php?page=$1 last;
            rewrite ^/story/([0-9]+)/modify/([a-z]+)/?$ /admin/linkadmin.php?id=$1&action=$2 last;
            rewrite ^/view/([^/]+)/?$ /admin/admin_users.php?mode=view&user=$1 last;

            ## Groups
            rewrite ^/join_group/action/(published|queued|discard)/link/(\d+)/?$ /join_group.php?action=$1&link=$2 last;
            rewrite ^/groups/?$ /groups.php last;
            rewrite ^/groups/search/([^/]+)/?$ /groups.php?view=search&keyword=$1 last;
            rewrite ^/groups/submit/?$ /submit_groups.php last;
            rewrite ^/groups/(members|name|oldest|newest)/?$ /groups.php?sortby=$1 last;
            rewrite ^/groups/([^/]+)/?$ /group_story.php?title=$1 last;
            rewrite ^/groups/([^/]+)/page/([0-9]+)/?$ /group_story.php?title=$1&page=$2 last;
            rewrite ^/groups/([^/]+)/?$ /group_story.php?title=$1&view=published last;
            rewrite ^/groups/([^/]+)/(published|upcoming|shared|members)/?$ /group_story.php?title=$1&view=$2 last;
            rewrite ^/groups/([^/]+)/(published|upcoming|shared|members)/page/([0-9]+)/?$ /group_story.php?title=$1&view=$2&page=$3 last;
            rewrite ^/groups/([^/]+)/(published|upcoming|shared|members)/category/([^/]+)/?$ /group_story.php?title=$1&view=$2&category=$3 last;
            rewrite ^/groups/([^/]+)/(published|upcoming|shared|members)/category/([^/]+)/page/([0-9]+)?$ /group_story.php?title=$1&view=$2&category=$3&page=$4 last;
            rewrite ^/groups/delete/([0-9]+)/?$ /deletegroup.php?id=$1 last;
            rewrite ^/groups/edit/([0-9]+)/?$ /editgroup.php?id=$1 last;
            rewrite ^/groups/id/([0-9]+)/?$ /group_story.php?id=$1 last;
            rewrite ^/groups/join/([0-9]+)/? /join_group.php?id=$1&join=true last;
            rewrite ^/groups/member/admin/id/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=admin&userid=$3 last;
            rewrite ^/groups/member/normal/id/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=normal&userid=$3 last;
            rewrite ^/groups/member/moderator/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=$2&userid=$3 last;
            rewrite ^/groups/member/flagged/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=flagged&userid=$3 last;
            rewrite ^/groups/member/banned/id/([0-9]+)/role/([a-zA-Z0-9_-]+)/userid/([0-9]+)/?$ /groupadmin.php?id=$1&role=banned&userid=$3 last;
            rewrite ^/groups/page/([0-9]+)/?$ /groups.php?page=$1 last;
            rewrite ^/groups/unjoin/([0-9]+)/? /join_group.php?id=$1&join=false last;
            rewrite ^/groups/withdraw/([0-9]+)/user_id/([0-9]+)/?$ /join_group.php?group_id=$1&user_id=$2&activate=withdraw last;
           
            ## Live
            rewrite ^/live/?$ /live.php last;
            rewrite ^/live/comments/?$ /live_comments.php last;
            rewrite ^/live/comments/page/([^/]+)/?$ /live_comments.php?page=$1 last;
            rewrite ^/live/published/?$ /live_published.php last;
            rewrite ^/live/published/page/([^/]+)/?$ /live_published.php?page=$1 last;
            rewrite ^/live/upcoming/?$ /live_unpublished.php last;
            rewrite ^/live/upcoming/page/([^/]+)/?$ /live_unpublished.php?page=$1 last;

            #Modules
            rewrite ^/inbox/?$ /module.php?module=simple_messaging&view=inbox last;
            rewrite ^/sitemapindex.xml /module.php?module=xml_sitemaps_show_sitemap last;
            rewrite ^/sitemap-([a-zA-Z0-9]+).xml /module.php?module=xml_sitemaps_show_sitemap&i=$1 last;
            rewrite ^/status/([0-9]+)/?$ /modules/status/status.php?id=$1 last;
            rewrite ^/toolbar/(\d+)/?$ /modules/pligg_web_toolbar/toolbar.php?id=$1;
            rewrite ^/sitemapindex.xml /module.php?module=xml_sitemaps_show_sitemap last;
            rewrite ^/sitemap-([0-9a-z]+).xml /module.php?module=xml_sitemaps_show_sitemap&i=$1 last;
     
            ## Pages
            rewrite ^/about/?$ /page.php?page=about last;
            rewrite ^/static/([^/]+)/?$ /page.php?page=$1 last;

            ## Pagination
            rewrite ^/category/([^/]+)/page/([^/]+)/?$ /index.php?category=$1&page=$2 last;
            rewrite ^/page/([^/]+)/?$ /index.php?page=$1 last;
            rewrite ^/page/([^/]+)/([^/]+)category/([^/]+)/?$ /index.php?page=$1&part=$2&category=$3 last;
            rewrite ^/upcoming/([^/]+)/page/(\d+)/?$ /upcoming.php?category=$1&page=$2 last;
            rewrite ^/published/page/([^/]+)/([^/]+)/?$ /index.php?page=$1&category=$2 last;
            rewrite ^/published/page/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1&part=$2&category=$3 last;
            rewrite ^/published/page/([^/]+)/?$ /index.php?page=$1 last;
            rewrite ^/published/page/([^/]+)/category/([^/]+)/?$ /index.php?page=$1&category=$2 last;
            rewrite ^/published/page/([^/]+)/([^/]+)category/([^/]+)/?$ /index.php?page=$1&part=$2&category=$3 last;
            rewrite ^/published/page/([^/]+)/([^/]+)/?$ /index.php?page=$1&part=$2 last;
            rewrite ^/published/page/([^/]+)/range/([^/]+)/?$ /?page=$1&range=$2 last;
            rewrite ^/search/page/([^/]+)/([^/]+)/?$ /search.php?page=$1&search=$2 last;
            rewrite ^/topusers/page/([^/]+)/?$ /topusers.php?page=$1 last;
            rewrite ^/topusers/page/([^/]+)/sortby/([^/]+)?$ /topusers.php?page=$1&sortby=$2 last;
            rewrite ^/user/page/([^/]+)/([^/]+)/([^/]+)/?$ /user.php?page=$1&view=$2&login=$3 last;
            rewrite ^/user/([^/]+)/([^/]+)/page/(\d+)/?$ /user.php?page=$3&view=$1&login=$2 last;
           
            ## Sort
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/?$ /index.php?part=$1 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/category/([^/]+)/?$ /index.php?part=$1&category=$2 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/page/(\d+)/?$ /index.php?part=$1&page=$2 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/category/([^/]+)/page/(\d+)/?$ /index.php?part=$1&category=$2&page=$3 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/([^/]+)/?$ /upcoming.php?part=$1&category=$2 last;
            rewrite ^/upcoming/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/([^/]+)/page/(\d+)/?$ /upcoming.php?part=$1&category=$2&page=$3 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/([^/]+)/?$ /index.php?part=$1&category=$2 last;
            rewrite ^/(year|month|week|today|yesterday|recent|alltime|upvoted|downvoted|commented)/([^/]+)/page/(\d+)/?$ /index.php?part=$1&category=$2&page=$3 last;

            ## URL Method 2 End ##
    }

    if (!-e $request_filename){
            rewrite ^/([^/]+)/?$ /index.php?category=$1 last;
    }
    if (!-e $request_filename){
            rewrite ^/([^/]+)/page/([^/]+)/?$ /index.php?category=$1&page=$2 last;
    }
    if (!-e $request_filename){
            rewrite ^/([^/]+)/([^/]+)/?$ /story.php?title=$2&category=$1 last;
            rewrite ^/([^/]+)/([^/]+)/([0-9]+)/?$ /story.php?title=$2&category=$1&comment_id=$3 last;
            rewrite ^/([^/]+)/([^/]+)/reply/([0-9]+)/?$ /story.php?title=$2&category=$1&comment_id=$3&reply=1 last;
    }

    if ($query_string ~* 'mosConfig_[a-zA-Z_]{1,21}(=|\%3D)' ){
     return 405;
    }
    if ($query_string ~* base64_encode.*\(.*\) ){
     return 405;
    }
    if ($query_string ~* (\<|%3C).*script.*(\>|%3E) ){
     return 405;
    }
    if ($query_string ~* 'GLOBALS(=|\[|\%[0-9A-Z]{0,2})' ){
     return 405;
    }
    if ($query_string ~* '_REQUEST(=|\[|\%[0-9A-Z]{0,2})' ){
     return 405;
    }

    if ($http_user_agent ~* ^pycurl/){
            return 405;
    }

}

    location ~ \.php$ {
        root           /usr/share/nginx/htdocs/example.com;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location /settings.php {
        deny all;
    }
    location ~ /\.(tpl|log)$ {
        deny all;
    }
}