##使用环境说明在centos7系统下使用nginx处理图片资源的访问,需要多个站点对应多个目录。##安装ingnx#安装源(nginx对应的软件仓库)#当前已是root权限下不用加sudoyum i
##使用环境说明在centos7系统下使用nginx处理图片资源的访问,需要多个站点对应多个目录。
##安装ingnx
#安装源(nginx对应的软件仓库)#当前已是root权限下不用加sudoyum install epel-releaseyum install nginx
配置nginx
可以直接在 /etc/nginx/nginx.conf
直接添加配置,也可以一个站点一个配置,放在/etc/nginx/conf.d/
目录下
- 直接写在nginx.conf 中
vi /etc/nginx/nginx.conf# 添加 一个server(一个站点一个server)http{ ...... #导入配置文件(配置文件目录,通常一个站点 一个配置文件) include /etc/nginx/conf.d/*.conf; #站点1 server{ ...... } #站点2 server{ listen 80; #监听80端口, 可以重复 server_name img.test.com; #绑定域名 #定义路径(url) location / { #根目录 alias /usr/share/nginx/img.test.com; #指向的静态文件路径,本场景中为n图片路径 } location /static/ { #路径 /static/ alias /usr/share/nginx/img.test.com; #指向的静态文件路径,本场景中为n图片路径 } } }
- 单站点单文件写法
# create siteserver{ # 绑定端口和域名 listen 89; server_name img2.test.com; #路径 location /static/ { alias /usr/share/nginx/img.test.com; } #日志 access_log /var/log/nginx/img.test.com_access.log; error_log /var/log/nginx/img.test.com_error.log;}
- 验证配置文件
nginx -t
- 重启ingnx
systemctl restart nginx.service
打开对应端口的防火墙
......
测试访问
#因为没有开防火墙,这里直接查看端口 启动[root@iZm5ejfcqapyvgcfz2wdf7Z nginx]# telnet 127.0.0.1 88Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.^CConnection closed by foreign host.[root@iZm5ejfcqapyvgcfz2wdf7Z nginx]# [root@iZm5ejfcqapyvgcfz2wdf7Z nginx]# netstat -apnActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14876/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1328/sshd tcp 0 0 0.0.0.0:88 0.0.0.0:* LISTEN 14876/nginx: master tcp 0 0 0.0.0.0:89 0.0.0.0:* LISTEN 14876/nginx: master tcp 0 0 118.190.137.247:22 61.135.37.202:23251 ESTABLISHED 2037/sshd: root@pts tcp 0 0 118.190.137.247:57644 140.205.140.205:80 ESTABLISHED 1865/AliYunDun tcp 0 36 118.190.137.247:22 61.135.37.202:23416 ESTABLISHED 2069/sshd: root@pts tcp 0 0 127.0.0.1:88 127.0.0.1:50434 TIME_WAIT - tcp6 0 0 :::80 :::* LISTEN 14876/nginx: master udp 0 0 118.190.137.247:123 0.0.0.0:* 904/ntpd udp 0 0 10.31.74.253:123 0.0.0.0:* 904/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 904/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 904/ntpd udp6 0 0 :::123 :::* 904/ntpd
结束
......