本文共 10137 字,大约阅读时间需要 33 分钟。
title: nginx的安装配置详解
tags: nginx,虚拟服务器,curlyum install bind-utils -y
yum install curl -y
进行安装。yum groupinstall "Development tools"
方式进行安装。[root@maiyat conf]# rpm -qa pcrepcre-7.8-7.el6.x86_64[root@maiyat conf]# rpm -qa pcre-develpcre-devel-7.8-7.el6.x86_64
[root@maiyat conf]# rpm -qa opensslopenssl-1.0.1e-57.el6.x86_64[root@maiyat conf]# rpm -qa openssl-developenssl-devel-1.0.1e-57.el6.x86_64[root@maiyat conf]#
wget http://nginx.org/download/nginx-1.6.3.tar.gztar xf nginx-1.6.3.tar.gz cd nginx-1.6.3./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_moduleecho $?make && make installuseradd nginx -s /sbin/nologin -Mid nginxgrep nginx /etc/groupln -s /application/nginx-1.6.3/ /application/nginx
yum groupinstall "Development tools"
如果是源码安装pcre的话,安装nginx可能会找不到pcre,而提示无法找到pcre,因此编译的时候,我们必须用--with=/usr/local/bin/pcre 指定pcre的位置。[root@maiyat ~]# /application/nginx/sbin/nginx [root@maiyat ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 1382 root 6u IPv4 13412 0t0 TCP *:http (LISTEN)nginx 1383 nginx 6u IPv4 13412 0t0 TCP *:http (LISTEN)[root@maiyat ~]# ps -ef |grep nginx |grep -v greproot 1382 1 0 23:24 ? 00:00:00 nginx: master process /application/nginx/sbin/nginxnginx 1383 1382 0 23:24 ? 00:00:00 nginx: worker process [root@maiyat ~]# [root@maiyat ~]# ss -lntup |grep nginxtcp LISTEN 0 128 *:80 *:* users:(("nginx",1382,6),("nginx",1383,6))
[root@maiyat sbin]# /application/nginx/sbin/nginx -tnginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
[root@maiyat sbin]# /application/nginx/sbin/nginx -s reload[root@maiyat sbin]#
[root@maiyat sbin]# /application/nginx/sbin/nginx -Vnginx version: nginx/1.6.3built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) TLS SNI support enabledconfigure arguments: --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module[root@maiyat sbin]#
[root@maiyat nginx]# ls |grep -v temp |xargs tree -L 1conf 配置文件|--nginx.confhtml 站点目录|-- 50x.html`-- index.htmllogs 日志信息|-- access.log|-- error.log`-- nginx.pidsbin 执行文件`-- nginx
nginx的配置文件nginx.conf,我们配置的时候可以先把原来的备份,然后借用nginx.conf.default过来直接配置,配置文件一定要注意{},必须成对出现,不能拆散如:
cat nginx.conf##################################################user nobody;worker_processes 1;(一般和服务器的核数一样)#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;这几行属于main区,Nginx核心功能模块events { worker_connections 1024; 一个worker所能处理的多少的并发连接数14 }这几行属于event区,Nginx核心功能模块http { include mime.types; default_type application/octet-stream;这个属于http区开始,Nginx核心功能模块##################################################实例搭建两个虚拟主机www.maiyat.com bbs.maiyat.com##################################################cp nginx.conf nginx.conf.bakegrep -v "#|^$" nginx.conf.default > nginx.confworker_processes 1; worker进程的数量events { 事件区块的开始worker_connections 1024; 每个woker进程支持的最大连接数} 事件区块结束http { http区块开始include mime.types; Nginx支持的媒体类型库文件包含default_type application/octet-stream; 默认的媒体类型sendfile on; 开启高效传输模式keepalive_timeout 65; 连接超时server { 第一个server区块开始,代表一个独立的虚拟主机站点 listen 80; 提供服务的端口,默认是80 server_name www.maiyat.com; 提供服务的域名主机名 location / { 第一个Location区块的开始 root html/www; 站点的根目录,相对路径,相对于Nginx的安装路径 index index.html index.htm; 默认的首页文件,多个用空格分开 } 第一个区块结束} server { listen 80; server_name bbs.maiyat.com; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; 出现对应的http状态码使用50x.html回应 location = /50x.html { Location区块的开始,访问50x.html root html; 指定对应的站点目录为html }}} http区块结束
[root@maiyat conf]# cat /etc/hosts#127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6127.0.0.1 maiyat www.maiyat.com bbs.maiyat.com blog.maiyat.com192.168.50.2 maiyat www.maiyat.com bbs.maiyat.com blog.maiyat.com
3.2 如果是windows的hosts文件在 C:\Windows\System32\drivers\etc
3.3 linux下用curl命令看能不能返回消息,windows下用浏览器输入域名[root@maiyat conf]# curl www.maiyat.comhello world[root@maiyat conf]# curl bbs.maiyat.comhello oldboy[root@maiyat conf]# curl blog.maiyat.comhappy comeon maiyat.com !
listen 80,80
改为8001或者其他的就好了,其他的都不用改,注意要把几个虚拟主机的域名统一,需要重启nginx服务。[root@maiyat conf]# vi nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8001; server_name www.maiyat.com; location / { root html/www; index index.html index.htm; }} server { listen 8002; server_name www.maiyat.com; location / { root html/bbs; index index.html index.htm; } }server { listen 8003; server_name www.maiyat.com; location / { root html/blog; index index.html index.htm; } }}[root@maiyat conf]# cd ../sbin/[root@maiyat sbin]# ./nginx -tnginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful[root@maiyat sbin]# ./nginx -s reload[root@maiyat sbin]# lsof -i :8001COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 1382 root 10u IPv4 13964 0t0 TCP *:vcom-tunnel (LISTEN)nginx 1476 nginx 10u IPv4 13964 0t0 TCP *:vcom-tunnel (LISTEN)[root@maiyat sbin]# lsof -i :8002COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 1382 root 11u IPv4 13965 0t0 TCP *:teradataordbms (LISTEN)nginx 1476 nginx 11u IPv4 13965 0t0 TCP *:teradataordbms (LISTEN)[root@maiyat sbin]# lsof -i :8003COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 1382 root 12u IPv4 13966 0t0 TCP *:mcreport (LISTEN)nginx 1476 nginx 12u IPv4 13966 0t0 TCP *:mcreport (LISTEN)[root@maiyat sbin]# curl www.maiyat.com:8001hello world[root@maiyat sbin]# curl www.maiyat.com:8002hello oldboy[root@maiyat sbin]# curl www.maiyat.com:8003happy comeon maiyat.com !
2 基于ip进行配置虚拟主机,注意修改的时候把端口恢复为80。
[root@maiyat sbin]# cd ../conf[root@maiyat conf]# vi nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 192.168.50.250:80; server_name www.maiyat.com; location / { root html/www; index index.html index.htm; }} server { listen 192.168.50.251:80; server_name www.maiyat.com; location / { root html/bbs; index index.html index.htm; } }server { listen 192.168.50.253:80; server_name www.maiyat.com; location / { root html/blog; index index.html index.htm; } }}[root@maiyat conf]# [root@maiyat conf]# cd ../sbin/[root@maiyat sbin]# ./nginx -tnginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is oknginx: [emerg] bind() to 192.168.50.250:80 failed (99: Cannot assign requested address)nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test failed[root@maiyat sbin]# ip addr add 192.168.50.250/24 dev eth0 [root@maiyat sbin]# ip addr add 192.168.50.251/24 dev eth0 [root@maiyat sbin]# ip addr add 192.168.50.253/24 dev eth0 [root@maiyat sbin]# ./nginx -tnginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful[root@maiyat sbin]# ./nginx -s reload[root@maiyat sbin]# curl 192.168.50.250hello world[root@maiyat sbin]# curl 192.168.50.251hello oldboy[root@maiyat sbin]# curl 192.168.50.253happy comeon maiyat.com
转载于:https://blog.51cto.com/ouyangtao/2137227