首页 > Linux操作系统 > Linux操作系统 > nginx 源码安装
下载和文档
http://nginx.org/en/download.html
tengine https://tengine.taobao.org/
#down source
cd /usr/server/src
wget -c http://nginx.org/download/nginx-1.12.2.tar.gz
tar xzvf nginx-1.12.2.tar.gz
2.编译和安装
2.1安装Nginx时必须先安装相应的编译工具:
#yum -y install gcc gcc-c++ autoconf automake
#yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
zib :Nginx提供gzip模块,需要zlib库支持
openssl: Nginx提供ssl功能
pcre: 支持地址重写rewrite功能
2.2建立系统组合用户(-r):
group -r nginx 创建系统组(-r)
useradd -s /sbin/nologin -g nginx -r nginx 不能登陆的系统用户
2.3#install dir
mkdir /usr/server/nginx
2.4#编译
cd /usr/server/src/nginx-1.12.2
./configure \
--prefix=/usr/server/nginx \
--sbin-path=/usr/server/nginx/sbin/nginx \
--conf-path=/usr/server/nginx/conf/nginx.conf \
--error-log-path=/usr/server/nginx/logs/error.log \
--pid-path=/usr/server/nginx/nginx.pid \
--lock-path=/usr/server/nginx/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--http-log-path=/usr/server/nginx/logs/access.log \
--http-client-body-temp-path=/usr/server/nginx/client_body_temp \
--http-proxy-temp-path=/usr/server/nginx/proxy_tmp \
--http-fastcgi-temp-path=/usr/server/nginx/fcgi_tmp \
--with-http_stub_status_module
2.5安装
make && make install
2.6 启动
/usr/server/nginx/sbin/nginx -c /usr/server/nginx/conf/nginx.conf
ps -ef|grep nginx
netstat -tulnp |grep nginx
停止/重新加载/stop, quit, reopen, reload
/usr/server/nginx/sbin/nginx -s reload
/usr/server/nginx/sbin/nginx -h
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/server/nginx/)
-c filename : set configuration file (default: /usr/server/nginx/conf/nginx.conf)
-g directives : set global directives out of configuration file
3. 配置文件
结构
vi nginx.conf
...
events{
...
}
http{
...
server{
… #这些server可以放在单个的配置文件中加载
}
server{
...
}
}
具体配置如下(加载多个虚拟主机vhost):
vi nginx.conf
#user nobody;
worker_processes 2; 一般是cpu的个数
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"';
# $remote_addr 中转IP
# $http_x_forwarded_for 客户端真实IP
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/x-javascript;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_temp_file_write_size 64k;
include vhost/*.conf;
}
-------------------
#可以配置多个虚拟主机,使用相同的端口(如80),通过域名server_name区分
#下面是其中一个虚拟主机的配置
------------------
cd vhost
vi api.conf
upstream api-tqflow_host {
server 10.30.134.248:8080 weight=1;
#server 10.26.47.25:8080 weight=0;
}
server {
listen 80;
server_name tqflow.170ds.com api.tqflow.170ds.com;
root /home/portalweb/htdocs/html/O2O;
rewrite ^/$ /index.html redirect;
location ~ ^/api/ {
proxy_set_header HOST $host;
proxy_set_header X-REAL-IP $remote_addr;
proxy_set_header X-Forwareded-For $proxy_add_x_forwarded_for;
proxy_pass http://api-tqflow_host$request_uri;
}
access_log /usr/servers/nginx/logs/access_tqtmall.log main;
error_log /usr/servers/nginx/logs/error_tqtmall.log;
}
Nginx 的匹配规则(1):
location
语法:location [=|~|~*|^~|] /uri/ {...}
默认值: no
使用字段:server
这个自动根据URI的不同需求来配置,可以使用字符串和正则表达式匹配,如果要使用正则表达式,必须指定如下前缀:
~: 区分大小写
~*: 不区分大小写
^~: 禁止表达式匹配
=: 精确匹配
location =/ {
#只匹配 / 的查询
[configure A]
}
location / {
# 匹配任何以 / 开始的查询,但正则表达式与一些较长的字符串将被首先匹配。
[configure B]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。
[configure C]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配任何以gif,jpg,jpeg结尾的文件,但是所有/images/目录将在 configure C 中处理。
[configure D]
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/807718/viewspace-2153392/ ,如需转载,请注明出处,否则将追究法律责任。