安装
Ubuntu
apt-get install nginx
CentOS
# 1. 安装相关依赖 yum install gcc -c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel # 2. 下载nginx包、解压、进入 wget -c https://nginx.org/download/nginx-1.10.1.tar.gz tar -zxvf nginx-1.10.1.tar.gz cd nginx-1.10.1 # 3. 编译 ./configure # 这个命令会在目录里生成Makefile文件 make make install # 安装路径为/usr/local/nginx # 执行文件路径/usr/local/nginx/sbin/nginx,可以做软连接 # 启动、停止、关闭命令 nginx -s reload nginx -s stop nginx -s quit
相关命令
相关目录
# 主目录 /usr/local/nginx # docker部署的nginx的主目录默认为/etc/nginx # 配置文件路径 /usr/local/nginx/conf/nginx.conf # linux 安装目录\conf\nginx.conf # Windows # 静态文件路径 /usr/local/nginx/html/
启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf # 看到如下显示说明启动成功 ''' nginx.conf syntax is ok nginx.conf test is successful '''
重启
/usr/local/nginx/sbin/nginx -s reload
查看进程是否启动
ps -ef|grep nginx
查看端口号是否占用
netstat -tunlp|grep 端口号
强制停止
pkill -9 nginx
验证nginx配置文件是否正确
/usr/local/nginx/sbin/nginx -t # 看到如下显示说明配置文件正确 ''' nginx.conf syntax is ok nginx.conf test is successful '''
Nginx配置基于多域名、端口、IP的虚拟主机
基于域名的虚拟主机
通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站使用的都是基于域名的主机
配置:修改nginx配置文件配置多域名,重启nginx服务,创建对应的不同站点目录并上传站点文件,也可都使用一个站点目录,通过多域名来访问

server{ listen 80; server_name www.aaa.com; location / { root html/aaa; index index.html index.htm; } } server{ listen 80; server_name www.bbb.com; location / { root html/bbb; index index.html index.htm; } } server{ listen 80; server_name www.ccc.com; location / { root html/ccc; index index.html index.htm; } }
基于端口的虚拟主机
通过不同的端口来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部的网站,例如:一些不希望直接对外提供用户访问的网站后台等,访问基于端口的虚拟主机,地址里要带有端口号,例如http://www.test.com:81 http://www.test.com:82等

server{ listen 80; server_name localhost; location / { root html/aaa; index index.html index.htm; } } server{ listen 81; server_name localhost; location / { root html/bbb; index index.html index.htm; } } server{ listen 82; server_name localhost; location / { root html/ccc; index index.html index.htm; } }
基于IP的虚拟主机
通过不同的IP区分不同的虚拟主机,此类虚拟主机对应的企业应用非常少见,一般不同的业务需要使用多IP的场景都会在负载均衡上进行IP绑定。

server{ listen 10.0.0.7:80; server_name www.aaa.com; location / { root html/aaa; index index.html index.htm; } } server{ listen 10.0.0.8:80; server_name www.bbb.com; location / { root html/bbb; index index.html index.htm; } } server{ listen 10.0.0.9:80; server_name www.ccc.com; location / { root html/ccc; index index.html index.htm; } }
其他配置
修改上传文件大小限制
Nginx 默认是对上传的文件大小有限制的,我们可以修改相应配置来修改对文件大小的限制
# 在http{}段落增加如下参数 client_max_body_size 20M; # 20M可以换为任何大小
参考:https://www.cnblogs.com/ssgeek/p/9220922.html