342423 发表于 2017-9-1 12:58:03

nginx1.9.2编译安装

一、nginx安装过程

rpm -qa gcc-c++

rpm -qa pcre pcre-devel###(nginx的URL重写rewrite模块用到,pcre就用来干这个)

rpm -qa openssl openssl-devel(加密的       https443端口是加密的http服务)

yum install openssl openssl-devel -y(https加密的时候要用到)

useradd nginx -s /sbin/nologin -M

cd /tools/nginx-1.9.2

./configure --prefix=/application/nginx-1.9.2/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module######(--prefix=PATH 设置安装路径

--user=USER进程用户权限 --group=GROUP进程用户组权限 --with-http_stub_status_module激活状态信息--with-http_ssl_module 激活ssl功能)

make

make install

/application/nginx-1.9.2/sbin/nginx -t 检查语法

查看参数 /application/nginx/sbin/nginx -V

有错误可以去/application/nginx/logs/error.log查看


详解nginx.conf

cd /application/nginx/conf/

egrep -v "#|^$" nginx.conf.default后查看有用信息

worker_processes 1;##worker进程数量

(error_log logs/error.log) error;##日志在application/nginx/logs/error.log#相对路径默认存在

events {##时间区块开始

worker_connections 1024;##每个worker进程支持最大的连接数

}

http {##http区块开始

include mime.types;##nginx支持的媒体类型库文件包含

default_type application/octet-stream;##默认的媒体类型

sendfile on;##开启高效传输模式

keepalive_timeout 65;##连接超时


server {##第一个server区块开始,表示一个独立的虚拟主机站点

listen 80;##提供服务的端口,默认8当

server_name localhost;##提供服务的域名主机名可加别名

location / {##第一个location区块开始

root html/www;##站点的根目录,相对于nginx安装目录

index index.html index htm;##默认的首页文件,多个用空格分开

}#第一个location区块结果

error_page 500 502 503 504 /50x.html;##出现对应的http状态码试用50x.html回应

location = /50x.html {##location区块开始,访问50x.html,

root html;

}

}

}


二、搭建基于域名的主机实战

1/配置nginx.conf

cd /application/nginx/conf/

egrep -v "^$|#" nginx.conf.defult >nginx.conf

vim nginx.conf


http {

   include       mime.types;

   default_typeapplication/octet-stream;

   sendfile      on;

   keepalive_timeout65;


server {

       listen       80;

       server_namewww.liangtiantian.org;

       location / {

         root   html/www;

         indexindex.html index.htm;

       }   

}

}

mkdir /application/nginx/html/www -p

echo "www.liangtiantian====>>192.168.88.90" >html/www/index.html


2、检查语法,重新加载nginx

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

3、配置hosts。测试

vim /etc/hosts

192.168.88.89 www.iyunv.com bbs.iyunv.com

curl www.iyunv.com

windows:

在C:\Windows\System32\drivers\etc\hosts

加入192.168.88.89 www.iyunv.com bbs.iyunv.com

浏览器里访问www.iyunv.com




拓展:

a、基于端口的虚拟主机


server {

       listen       8888;

       server_namewww.liangtiantian.org;

       location / {

         root   html/www;

         indexindex.html index.htm;

       }   

}


/application/nginx/sbin/nginx -t    检测语法

/application/nginx/sbin/nginx -s reload    平滑加载

http://www.liangtiantian.org:8888


b、优化

1.include分割各个模块

在http模块

http {##http区块开始

include mime.types;##nginx支持的媒体类型库文件包含

default_type application/octet-stream;##默认的媒体类型

sendfile on;##开启高效传输模式

keepalive_timeout; 65;##连接超时

include extra/*.conf;###文件放在conf下面

}

mkdir extra

cp nginx.conf nginx.conf.base-name

sed -n "11,18p" nginx.conf >extra/www.conf

sed -n "20,27p" nginx.conf >extra/blog.conf

sed -i "10,44d" nginx.conf

sed -i '10 i include extra/www.conf;\ninclude extra/blog.conf;' nginx.conf

../sbin/nginx -s reload

2.Nginx status模块配置

作用:提供服务状态信息

模块:http_stub_status_module

cat >>/application/nginx/conf/extra/status.conf<<EOF

##status

server{

listen 80;

server_name status.iyunv.com;

location /{

stub_status on;

access_log         off;

allow 10.0.0.0/24;

deny all;

}

}

EOF

C:\Windows\System32\drivers\etc\hosts 文件更改解析

主机解析不要忘记

查看status.iyunv.com


3.增加错误日志(error_log)

error_log         file             level;

关键字      日志文件 错误日志级别



4.rewrite301跳转

需要pcre支持

在http中添加server新模块

server {

listen 80;

server_name iyunv.com;

rewrite ^/(.*) http://www.iyunv.com/$1 permanent;

}
页: [1]
查看完整版本: nginx1.9.2编译安装