一、安装
apache iis
linux内的负载均衡软件:lvs
淘宝:tengine
1、下载
wget http://nginx.org/download/nginx-1.16.1.tar.gz
2、解压
tar xf nginx-1.16.1.tar.gz
3、预安装
#./configure --help
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module
4、下载配套软件
yum install -y gcc
yum install -y pcre-devel
yum install -y openssl-devel
5、安装
make && make install
二、配置
我的安装位置/opt/nginx2
1、安装完成后会在安装位置生成如下目录结构
Conf 配置文件
html 存放静态文件 index.html默认的欢迎页面
logs 日志目录
sbin 二进制文件
启动以后会生成一个主进程,根据配置文件生成子进程(工作进程),主进程不负责处理用户的请求,只负责转发用户的请求,真正处理用户请求的是子进程。
2、查看帮助
./sbin/nginx -h
[root@iZbp1j8qub2jsx5txhzkxoZ ngnix]# ./sbin/nginx -h
nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /opt/ngnix/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
3、启动
./sbin/nginx
4、查看是否已经在运行
ps -ef|grep nginx
5、查看端口
ss -tnlp
6、清空(关闭防火墙):
iptables -F
如果是阿里云上的服务器,需要对防火墙进行设置,容许服务端进行访问的端口。
7、修改配置文件
vim conf/nginx.conf
8、重启服务
./sbin/nginx -s reload
三、nginx.conf配置文件介绍:
1、root和alias的区别
location /img {
root /opt/test2;
index index.html index.htm;
}
location /img {
alias /opt/test2;
index index.html index.htm;
}
root 根,即/img是从root(/opt/test2)延续之意,所以文件目录必须设成/opt/test2/img
alias 别名,/opt/test2是/img的别名,文件目录设成/opt/test2即可,不需要跟/img
2、多域名
server_name localhost;
可以设置多域名以空格分隔
设置域名后若想在本地计算机上使用,可以修改windows内的hosts文件,谷歌浏览器和火狐浏览器默认会添加https,造成无法访问的问题,ie浏览器可以正常使用。
如果想不同域名对应不同页面:
server {
listen 80;
server_name taobao.com;
location / {
root /opt/test/taobao;
}
server {
listen 80;
server_name jd.com;
location / {
root /opt/test/jd;
}
若想设置用ip访问时的默认server,可以:
listen 80 default_server;
四、项目练习:
1、下载图片并重命名保存
wget http://xiaohuar.com/d/file/p/drpvcito2yx.jpg -O 1.jpg
Tips
1、因为把html写在了root文件夹内,普通用户通过浏览器连接服务器时不时root身份,所以无法取得访问权限。
解决方案:
1、修改配置文件,默认访问用户为root用户。
2、更改html放置文件夹,并保证权限可读,文件夹可执行。
2、复制百度图片的地址进行下载会出问题