首页 > 代码库 > 源码编译Nginx服务配置
源码编译Nginx服务配置
一、实验环境:
RHEL7.0 172.25.254.1 server1.example.com firewalld disable
二、实验内容:
1.源码安装Nginx
nginx-1.9.14.tar.gz 下载源码包
tar zxf nginx-1.9.14.tar.gz
cd nginx-1.9.14/
vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" #关闭debug(由于使用gcc编译器,所以关闭gcc编译时安装的debug功能)
vim src/core/nginx.h
#define NGINX_VER "nginx/" #隐藏nginx版本号
useradd -u 800 -M -d /usr/local/nginx -s /sbin/nologin nginx
yum install -y gcc prce-devel openssl-devel
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module
make 编译
make install 安装
ls /usr/local/nginx 编译安装完成后查看
conf html logs sbin
2.配置:
[root@server1 nginx-1.9.14]# cd /usr/local/nginx/
[root@server1 nginx]# vim conf/nginx.conf
worker_processes 1; #通过lscpu来查看有几个cpu
events {
use epoll; #采用异步非阻塞模式 apache --select 同步阻塞机制 io复用模型类型 worker_connections 1024;
}
[root@server1 nginx]# vim /etc/profile ##添加nginx执行路径
export PATH=$PATH:/usr/local/nginx/sbin
[root@server1 nginx]# source /etc/profile
[root@server1 nginx]# nginx -t ##检查nginx配置是否有误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# nginx ##启动nginx服务
nginx -s reload #重启
nginx -s stop #关闭
测试:
[root@server1 nginx]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/
Date: Wed, 14 Sep 2016 16:12:37 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 14 Sep 2016 15:37:42 GMT
Connection: keep-alive
ETag: "57d96ec6-264"
Accept-Ranges: bytes
[root@server1 nginx]# cd html/ 默认发布目录
[root@server1 html]# ls
50x.html index.html
网页测试:
3.添加HTTPS:
[root@server1 nginx]# vim conf/nginx.conf
# HTTPS server #开启HTTPS功能
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem; #修改证书名
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
}
[root@server1 nginx]# cd /etc/pki/tls/certs/
[root@server1 certs]# make cert.pem #新建证书
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shannxi
Locality Name (eg, city) [Default City]:xi‘an
Organization Name (eg, company) [Default Company Ltd]:redhat
Organizational Unit Name (eg, section) []:Linux
Common Name (eg, your name or your server‘s hostname) []:localhost
Email Address []:1210646568@qq.com
[root@server1 certs]# cp cert.pem /usr/local/nginx/conf/
[root@server1 certs]# nginx -t #检查配置是否有误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 certs]# nginx -s reload #重启服务
测试:
4.虚拟主机:
[root@server1 nginx]# vim conf/nginx.conf
在http {} 中添加
server {
listen 80;
server_name www.xiaoze.com;
location / {
root /virtual/xiaoze/html;
index index.html;
}
}
server {
listen 80;
server_name www.westos.com;
location / {
root /virtual/westos/html;
index index.html;
}
}
[root@server1 nginx]# mkdir -p /virtual/xiaoze/html
[root@server1 nginx]# mkdir -p /virtual/westos/html
[root@server1 nginx]# echo www.xiaoze.com > /virtual/xiaoze/html/index.html
[root@server1 nginx]# echo www.westos.com > /virtual/westos/html/index.html
[root@server1 nginx]# vim /etc/hosts
172.25.254.1 www.xiaoze.com
172.25.254.1 www.westos.com
[root@server1 nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# nginx -s reload
测试:
5.Nginx监控小插件(网站信息统计)
[root@server1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /message { #添加的内容
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
[root@server1 nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# nginx -s reload
测试:
6.网页重写(自动转到HTTPS)
[root@server1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name login.xiaoze.com;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
root /virtual/login/html;
index index.html;
}
}
[root@server1 nginx]# mkdir -p /virtual/login/html
[root@server1 nginx]# echo login.xiaoze.com > /virtual/login/html/index.html
[root@server1 nginx]# vim /etc/hosts
172.25.254.1 login.xiaoze.com
[root@server1 nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# nginx -s reload
测试:
本文出自 “需要努力的人” 博客,转载请与作者联系!
源码编译Nginx服务配置