##使用環境說明 在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
......測試