##使用环境说明 在centos7系统下使用nginx处理图片资源的访问,须要多个站点对应多个目录。nginx
##安装ingnxcentos
#安装源(nginx对应的软件仓库) #当前已经是root权限下不用加sudo yum install epel-release yum install nginx
能够直接在 /etc/nginx/nginx.conf
直接添加配置,也能够一个站点一个配置,放在/etc/nginx/conf.d/
目录下ssh
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 site server{ # 绑定端口和域名 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
systemctl restart nginx.service
......tcp
#由于没有开防火墙,这里直接查看端口 启动 [root@iZm5ejfcqapyvgcfz2wdf7Z nginx]# telnet 127.0.0.1 88 Trying 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 -apn Active 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
......测试