0, 先决条件
Nginx 依赖 zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre
安装命令:
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

pcre安装命令:
wget http://downloads.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz;
cd pcre-8.41;
./configure
make && make install
pcre-config --version

1,Nginx 安装
1.1, 下载 Nginx,下载地址:http://nginx.org/download/nginx-1.13.0.tar.gz
$ wget http://nginx.org/download/nginx-1.13.0.tar.gz
1.2, 解压安装包
$ tar -zxvf nginx-1.13.0.tar.gz
1.3, 进入安装包目录
$ cd nginx-1.13.0
1.4, 编译安装
$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-7.8
$ make
$ make install
1.5, 查看nginx版本
$ /usr/local/nginx/sbin/nginx -v
到此,nginx安装完成。

2, Nginx 配置
2.1, 创建 Nginx 运行使用的用户 www(可选)
$ /usr/sbin/groupadd www
$ /usr/sbin/useradd -g www www
2.2, 配置nginx.conf 默认路径:/usr/local/nginx/conf/nginx.conf
$ cat /usr/local/nginx/conf/nginx.conf
# 访问的用户
# user root;
# 设置值和CPU核心数一致
worker_processes 16;

# 日志位置和日志级别
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

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;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

gzip on;

upstream system_server {
server 127.0.0.1:8080;
keepalive 2000;
}

server {
listen 801;
# listen 443 ssl;
server_name 127.0.0.1;

#
# ssl on;
# ssl_certificate /usr/local/nginx/conf/ssl/server.crt;
# ssl_certificate_key /usr/local/nginx/conf/ssl/server.key;
# ssl_session_timeout 5m;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;

# charset koi8-r;

access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

# websocket
location ^~ /socket {
proxy_pass http://system_server;
proxy_http_version 1.1;
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_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

# 正则表达式匹配路径
location ~* ^/system/.*$ {
proxy_pass http://system_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

}

2.3,检查配置文件ngnix.conf的正确性命令:
$ /usr/local/webserver/nginx/sbin/nginx -t

3, 启动 Nginx
3.1 启动命令如下:
$ /usr/local/webserver/nginx/sbin/nginx
3.2 访问站点
从浏览器访问我们配置的站点ip:http://127.0.0.1:9090

4, 其它
4.1 常用命令:
# 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reload
# 重启 Nginx
/usr/local/webserver/nginx/sbin/nginx -s reopen
# 停止 Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop

4.2 location配置语法规则: location [=|^~|~|~*] /uri/ { … }
= 开头表示普通字符开头,匹配规则:精确匹配;
^~ 开头表示普通字符开头,匹配规则:uri 最长路径匹配;
~ 开头表示特殊字符开头,匹配规则:正则匹配(区分大小写);
~* 开头表示特殊字符开头,匹配规则:正则匹配(不区分大小写);
!~ 匹配规则:区分大小写不匹配的正则;
!~* 匹配规则:不区分大小写不匹配的正则;
/ 通用匹配,任何请求都会匹配到。

4.3 rewrite 重写规则语法: rewrite 正则 替换 标志位
4.3.1 正则表达式
^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$ ---->http://xxxx.com/images/aa/abc01/test.gif
#其中
$1=([a-z]{2}) #$1=aa
$2=([a-z0-9]{5}) #$2=abc01
$3=(.*) #$3=test
$4=(png|jpg|gif) #$4=gif
4.3.2 URI 重写
/data?file=$3.$4 # rewrite之后的query http://data?file=test.gif
4.3.3 尾部的标记 last return break
last 标记之后会从新loaction ,继续rewrite 最多10次;
break标记是直接跳槽rewrite和localtion 进行query的处理
return标记停止rewrite 处理指令,进而控制主HTTP 模块处理请求,也就是HTTP请求也不处理了,直接给client 返回(结合error0page)

注意:nginx不对url做编码。例如:请求:/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

4.4 开机启动

在/etc/init.d/nginx (没有nginx文件则新建)中输入如下命令:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
# official web https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
#
# chkconfig: -
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid # Source function library.
. /etc/rc.d/init.d/functions # Source networking configuration.
. /etc/sysconfig/network # Check that networking is up.
[ "$NETWORKING" = "no" ] && exit nginx="/usr/sbin/nginx"
prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() {
# make required directories
user=`$nginx -V >& | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V >& | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f `
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
} start() {
[ -x $nginx ] || exit
[ -f $NGINX_CONF_FILE ] || exit
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq ] && touch $lockfile
return $retval
} stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq ] && rm -f $lockfile
return $retval
} restart() {
configtest || return $?
stop
sleep
start
} reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
} force_reload() {
restart
} configtest() {
$nginx -t -c $NGINX_CONF_FILE
} rh_status() {
status $prog
} rh_status_q() {
rh_status >/dev/null >&
} case "$1" in
start)
rh_status_q && exit
$
;;
stop)
rh_status_q || exit
$
;;
restart|configtest)
$
;;
reload)
rh_status_q || exit
$
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit
esac

自定义编译安装的nginx,需要根据安装路径修改下面这两项配置:
nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。

5, FAQs
5.1 NGINX启动时提示错误
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法:
#64位OS
ln -s /usr/local/lib/libpcre.so.1 /lib64
#32位OS
ln -s /usr/local/lib/libpcre.so.1 /lib

Nginx的安装(笔记)的更多相关文章

  1. Ubuntu下快速部署安装 Nginx + PHP + MySQL 笔记

        先更新软件库 sudo apt-get update 安装 MySQL sudo apt-get install mysql-server 安装 Nginx sudo apt-get inst ...

  2. 吴裕雄--天生自然Django框架开发笔记:Django Nginx+uwsgi 安装配置

    Django Nginx+uwsgi 安装配置 使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,需要一个可以稳定而持续的服务器,比如 ...

  3. Nginx编译安装第三方模块http_substitutions_filter_module

    Nginx编译安装第三方模块http_substitutions_filter_module 分类:服务器技术  作者:rming  时间:-- . >>ngx_http_substitu ...

  4. nginx的安装和负载均衡例子(RHEL/CentOS7.4)

    首先安装RHEL/CentOS7.4 mini ,然后关闭防火墙和 selinux ,更新系统(参看配置linux使用本地yum安装源和Redhat7/CentOS7 关闭防火墙和 selinux两个 ...

  5. MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记

    MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记 说明 以root账户登录Linux操作系统,注意:本文中的所有命令行前面的 #> 表示命令行提示符 ...

  6. Ubuntu上通过nginx部署Django笔记

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式.今天在Ubuntu上使用Nginx部署Django服务,虽然不是第一次搞这个了,但是发现还是跳进了好多坑,g ...

  7. 基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记

    基于Ubuntu14.04系统的nvidia tesla K40驱动和cuda 7.5安装笔记 飞翔的蜘蛛人 注1:本人新手,文章中不准确的地方,欢迎批评指正 注2:知识储备应达到Linux入门级水平 ...

  8. Nginx服务安装配置

    1.Nginx介绍 Nginx是一个高性能的HTTP和反向代理服务器,由俄罗斯人开发的,第一个版本发布于2004年10月4日.Nginx由于出色的性能,在世界范围内受到了越来越多人的关注,其特点是占有 ...

  9. sublime 安装笔记

    sublime 安装笔记 下载地址 安装package control 根据版本复制相应的代码到console,运行 按要求重启几次后再按crtl+shift+p打开命令窗口 输入pcip即可开始安装 ...

  10. Django Nginx+uwsgi 安装配置

    使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttp ...

随机推荐

  1. 【fhq Treap】bzoj1500(听说此题多码上几遍就能不惧任何平衡树题)

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 15112  Solved: 4996[Submit][Statu ...

  2. [译]Pandas常用命令对照清单

    我们在内容中使用以下简写: df pandas的DataFrame对象 s pandas的Series对象 导入以下包开始 import pandas as pd import numpy as np ...

  3. Erlang epmd官方文档中文翻译

    本文含epmd简介及官方文档之翻译,文档地址 http://erlang.org/doc/man/epmd.html翻译时的版本 R19.1 中英文水平都不咋地,不通顺处海涵,就酱. 简介 Erlan ...

  4. 应用服务器GC回收常见问题总结

    近一段时间多次发现因GC问题造成系统性能问题(应用服务间歇性响应缓慢.应用服务器CPU占用较高等),在此总结一下: 1.代码中直接调用GC.Collect() 2.字符串等操作频繁的内存申请 3.频繁 ...

  5. 关于构造函数和原型prototype对象的理解

    构造函数     1.什么是构造函数 构造函数,主要用于对象创建的初始化,和new运算符一起用于创建对象,一个类可以有多个构造函数,因为函数名相同,所以只能通过参数的个数和类型不同进行区分,即构造函数 ...

  6. (亲测)躺着破解IDM下载权限,治疗不用破解补丁的强迫症们

    首先.如果触犯了某些规则权限,请原谅. 很早以前就做过这个的破解,挺实用的,我今天就把之前写的经验贴出来大家一起学习学习~~~ 今天利用这个方法破解了最新版,最终的效果如下所示:我不是来刷存在感的.只 ...

  7. BZOJ:4333: JSOI2012 智者的考验

    4333: JSOI2012 智者的考验 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 68  Solved: 18[Submit][Status][ ...

  8. noi 2016 游记

    先挖个坑..这回大概不会太监吧(大雾 day -2 下午起飞的飞机,晚上到了成都..把东西扔到旅馆后就组队外出觅食了... 街上人不多,逛了半天才发现一家卖本地小吃的小店. KPM:诶诶给我来碗酸辣粉 ...

  9. 2017西安网络赛 F

    f(cos(x))=cos(n∗x) holds for all xx. Given two integers nn and mm, you need to calculate the coeffic ...

  10. 支付宝当面付开发(java)

    支付宝当面付开发(java) 业务流程:       接入准备:   直接下载demo: https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7 ...