1. 程式人生 > >Linux下安裝Nginx並配置一個圖片服務器

Linux下安裝Nginx並配置一個圖片服務器

com log 僅支持 centos6 str 技術分享 .html agent remote

首先安裝nginx安裝環境

nginxC語言開發,建議在linux上運行,本教程使用Centos6.5作為安裝環境。

--> gcc

安裝nginx需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc

yum install gcc-c++

--> PCRE

PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫nginxhttp模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。

yum install -y pcre pcre-devel

註:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。

--> zlib

zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlibhttp包的內容進行gzip,所以需要在linux上安裝zlib庫。

yum install -y zlib zlib-devel

--> openssl

OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。

nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux

安裝openssl庫。

yum install -y openssl openssl-devel

然後安裝nginx

Nginx官網下載tar.gz格式的安裝包,這裏下載的是nginx-1.10.3版本,環境使用centos的虛擬機

1、將安裝包上傳,解壓,命令tar -zxvf nginx-1.10.3.tar.gz

2、自定義創建一個文件夾作為Nginx安裝目錄,這裏在home創建nginx文件夾

技術分享圖片

3、在解壓的文件夾(nginx-1.10.3)下執行./configure --prefix=/home/nginx 命令。
意思即配置安裝環境,將會把Nginx安裝到/home/nginx下;

4、編譯:在解壓的文件夾下先後執行make

make install 命令

執行make

技術分享圖片

然後執行make install

技術分享圖片

5、Nginx默認使用端口是80,這裏直接先把Nginx端口改為8088

vi /home/nginx/conf/nginx.conf,修改server的端口,並配置一個圖片服務器

#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       8080;#寫內網端口,訪問時用外網端口進行映射訪問
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ~ .*\.(gif|jpg|jpeg|png)$ {  
            expires 24h;  
            root /home/images/;#指定圖片存放路徑  
            access_log /home/nginx/logs/images.log;#圖片 日誌路徑  
            proxy_store on;  
            proxy_store_access user:rw group:rw all:rw;  
            proxy_temp_path         /home/images/;#代理臨時路徑
            proxy_redirect          off;  

            proxy_set_header        Host 127.0.0.1;  
            proxy_set_header        X-Real-IP $remote_addr;  
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;  
            client_max_body_size    10m;  
            client_body_buffer_size 1280k;  
            proxy_connect_timeout   900;  
            proxy_send_timeout      900;  
            proxy_read_timeout      900;  
            proxy_buffer_size       40k;  
            proxy_buffers           40 320k;  
            proxy_busy_buffers_size 640k;  
            proxy_temp_file_write_size 640k;  
            if ( !-e $request_filename)  
            {  
                 proxy_pass  http://127.0.0.1:8080;#代理訪問地址,和上面的端口一致  
            }  
        }

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache‘s document root
        # concurs with nginx‘s one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

serverlisten的端口改為8088,並增加一個location配置,用於訪問圖片文件,這一串配置有#號在前面的註釋掉的都可以刪掉,免得看起來又長又亂。

6、啟動命令: /home/nginx/sbin/nginx -c /home/nginx/conf/nginx.conf
打開防火墻對應端口供訪問,8088,當然也可以直接關了防火墻。
若修改了nginx.conf配置,則需要重啟才生效,命令:/home/nginx/sbin/nginx -s reload

7、訪問。
輸入ifconfig命令查看虛擬機ip,測試:

技術分享圖片

/home/images下放張圖片,測試訪問:

技術分享圖片

技術分享圖片

Linux下安裝Nginx並配置一個圖片服務器