Nginx是由俄罗斯人开发的HTTP服务器,功能很多,本文主要使用其提供web服务。安装方式为编译安装。本文使用的程序版本及系统环境如下:
操作系统:centos6.6 x86_64
Nginx:nginx-1.8.0
一、编译安装Nginx
需要先安装包组“Development tools”和“Server Platform Development”。另外因为默认的编译选项里支持URL重写功能,所以要安装pcre-devel。
groupadd -r nginx useradd -g nginx -r nginx 添加用户,让nginx的worker进程以nginx用户的身份运行。
# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/log/nginx.lock --with-http_ssl_module # make && make install
当然编译选项有很多,用于提供不同的功能,使用上述编译选项,仅能让nginx提供web服务,并提供对https协议的支持,其它众多选项还有待学习。
/usr/local/nginx//sbin/nginx
安装完成后即可启动nginx,并使用浏览器测试。
二、配置文件
正常运行必需的配置
user
pid
event{....}:event段的配置全局有效,配置内容可以为空,但“event{}”配置段必需存在。
性能相关的配置
性能相关配置有很多,需要参考官方文档。
作为web服务器时使用的配置
放在“http {
}”配置段中,由ngx_http_core_module引入。本文主要介绍http相关的配置段。
使用如下配置让主机能够提供HTTP服务,并熟悉,“location”“log”“访问控制”等的使用方法。
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.a.com;
location = /index.html {
root "/web/images";
index index.html;
}
location /images {
root "/web/images";
index index.html;
}
location / {
root "/web/a";
}
location ~* \.(txt|text)$ {
root "/web/txt";
}
error_page 404 =5050 /error.html;
location /sta {
root "/web";
index index.html;
auth_basic "nginx info";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
allow 172.16.0.0/16;
deny all;
access_log /usr/local/nginx/access.log;
error_log /usr/local/nginx/error.log;
}
}
}如果要配置nginx支持使用https,则需要进行如下配置
server {
listen 443 ssl;
server_name www.b.com;
ssl_certificate /etc/nginx/ssl/nginx.crt; #存放CA颁发的证书
ssl_certificate_key /etc/nginx/ssl/nginx.key; #存放私钥
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /vhosts/web1;
index index.html index.htm;
}
}
原文地址:http://hankniu.blog.51cto.com/6946350/1657948