#前置条件
sudo yum install yum-utils
#添加yum源或者下载rpm包
#yum方式,在/etc/yum.repo.d/目录下添加nginx.repo,内容如下
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
#重载yum
sudo yum-config-manager --enable nginx-mainline
#安装
sudo yum install nginx
[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
[root@centos ~]# uname -a
Linux tCentos7 3.10.0-123.13.1.el7.x86_64 #1 SMP Tue Dec 9 23:06:09 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@centos ~]# yum install vim wget lsof gcc gcc-c++ bzip2 -y
[root@centos ~]# yum install net-tools bind-utils -y
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@centos ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
[root@centos ~]# wget http://zlib.net/zlib-1.2.8.tar.gz
[root@centos ~]# wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
[root@centos ~]# wget http://www.canonware.com/download/jemalloc/jemalloc-3.6.0.tar.bz2
[root@centos ~]# yum install zlib-devel openssl-devel -y
[root@centos ~]# vim /etc/selinux/config
#屏蔽以下两行
#SELINUX=enforcing
#SELINUXTYPE=targeted
#添加以下一行
SELINUXTYPE=disabled
#保存,退出
#重启后,查询是否关闭(显示Disabled则表示关闭)
[root@centos ~]# shutdown -r now
[root@centos ~]# getenforce
Disabled
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf pcre-8.36.tar.gz
[root@centos ~]# cd pcre-8.36
[root@centos ~]# ./configure
[root@centos ~]# make && make install
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf openssl-1.0.1j.tar.gz
[root@centos ~]# cd openssl-1.0.1j
[root@centos ~]# ./config
[root@centos ~]# make && make install
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf zlib-1.2.8.tar.gz
[root@centos ~]# cd zlib-1.2.8
[root@centos ~]# ./configure
[root@centos ~]# make && make install
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar xjf jemalloc-3.6.0.tar.bz2
[root@centos ~]# cd jemalloc-3.6.0
[root@centos ~]# ./configure
[root@centos ~]# make && make install
[root@centos ~]# echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
[root@centos ~]# ldconfig
[root@centos ~]# groupadd www
[root@centos ~]# useradd -g www www -s /sbin/nologin
[root@centos ~]# mkdir -p /data/www
[root@centos ~]# chmod +w /data/www
[root@centos ~]# chown -R www:www /data/www
***如果没法创建用户,需要检查SELinux状态是否关闭
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf nginx-1.6.2.tar.gz
[root@centos ~]# cd nginx-1.6.2
[root@centos ~]# ./configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.36 --with-ld-opt="-ljemalloc"
[root@centos ~]# make && make install
[root@centos ~]# vim /opt/nginx/conf/nginx.conf
#修改前面几行为:
user www www;
worker_processes auto;
error_log logs/error.log crit;
pid logs/nginx.pid;
events{
use epoll;
worker_connections 65535;
}
找到,并修改 root 行的内容
location / {
root /data/www;
index index.html index.htm;
}
保存,退出
[root@centos ~]# vim /data/www/index.html
<html>
head><title>nginx index.html</title></head>
<body>
<h1>index.html</h1>
</body>
</html>
#保存,退出
[root@centos ~]# cd /opt/nginx
[root@centos ~]# ldconfig
[root@centos ~]# ./sbin/nginx -c /opt/nginx/conf/nginx.conf -t
#如果显示下面信息,即表示配置没问题
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
--prefix=path #安装目录指定,不指定默认为/usr/local/
--sbin-path 指向(执行)程序文件(nginx),不指定默认为prefix/sbin/nginx
--conf-path= 指向配置文件(nginx.conf)不指定默认为prefix/conf/nginx.conf(ps:yum安装在/etc/nginx/nginx.conf)
--error-log-path= 指向错误日志目录,不指定默认为prefix/logs/error.log
--pid-path= 指向pid文件(nginx.pid)这里pid文件是什么,你可以去了解下,常用
--lock-path= 指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)
--user= 指定程序运行时的非特权用户,正常来说,生产环境,最好不要使用root来运行服务
--group= 指定程序运行时的非特权用户组,同上
--builddir= 指向编译目录
server {
listen 80; #nginx监听的端口
server_name www.laowanglaji.com; #nginx监听的域名,我现在的dns服务这个域名指向的是10.10.1.1,服务放在192.168.12.21内网的服务器上面,现在的意思是,nginx监听本服务器上面80端口,一旦有访问域名为www.laowanglaji.com的请求,我就转发,转发规则在下面
charset utf-8; #设置字符集utf-8
location / { #对以/后面的地址进行负载
root html;
index index.jsp index.html index.htm;
proxy_pass http://192.168.12.21:8001; #这个就是转发的真是服务器所在的,192.168.12.21上面8001上面监听的服务
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#上面的这几行,将访问者的真实信息转发给目标服务器
client_max_body_size 500m;
client_body_buffer_size 512k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
https://www.cnblogs.com/qlqwjy/p/8536779.html这个讲的挺不错
原文:https://www.cnblogs.com/kanglindeboke/p/11253081.html