1. 程式人生 > >Nginx搭建圖片伺服器

Nginx搭建圖片伺服器

楔子

指定一個目錄存放圖片,現在需要將其指定給Nginx,使用Nginx作為伺服器來訪問。

Nginx配置

配置參考 位置B其實如果僅僅作為圖片訪問,Nginx 不配置其他,直接修改位置A的目錄即可

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8088;
        server_name  localhost;
		# 開啟檔案目錄後,編碼需要按照作業系統來設定,否則中文名顯示亂碼(window上一般是gbk,Linux一般是utf-8或者不設定)
        charset gbk;
		#開啟檔案目錄 nginx.conf
		#autoindex on;
		autoindex_exact_size off;#off 以KB,MB,GB顯示檔案大小;on 以 bytes顯示檔案大小;
		autoindex_localtime on;  #on 
        #access_log  logs/host.access.log  main;
		
		
		#位置B
		location ~ images{
		#位置B 也可以下面這種寫法
		#location /images/{
			#開啟檔案目錄(如果配置在外面,下面的“位置A” 也會按照檔案目錄顯示)
			#autoindex on;
			root D:/pic;
			index 110.png 0.jpg;
		}
		#位置B 結束
		# 位置A
        location / {
            root   html;
            index  index.html index.htm;
        }
 
    }
	server {
		listen 81;
		server_name 127.0.0.1;
		
		location / {
			proxy_pass http://ym;
		}
	}
	# 反向代理
	upstream ym{
		server 127.0.0.1:80;
	}
}

開啟檔案目錄 的效果

在這裡插入圖片描述

需要注意的是

在這裡插入圖片描述

錯誤

如果配置好出現錯誤,多半是路勁的問題。如下圖 在這裡插入圖片描述 如果出現上圖錯誤,去檢視 %Nginx_home%\logs\error.log,提示的實際路勁問題,對照本地路勁分析

Nginx 命名

nginx -h 檢視幫助命令


D:\soft\ngix\nginx-1.12.1>nginx.exe -h
nginx version: nginx/1.12.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 # 測試配置檔案nginx.conf 是否正確
  -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: NONE)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

nginx -s{ stop(停止Nginx), quit(完整有序的停止Nginx), reopen(重新開啟配置檔案), reload(重新載入配置檔案)}

Nginx 日誌檔案切割

  • 把舊檔案A拷貝到 指定目錄(這時 Nginx還會繼續在舊檔案A中寫日誌)
  • 呼叫nginx -s reopen ,nginx日誌就會寫入新的檔案中 動態演示圖如下 在這裡插入圖片描述