nginx是C語言開發,建議在linux上運行,本教程使用Centos6.5做爲安裝環境。php
--> gcchtml
安裝nginx須要先將官網下載的源碼進行編譯,編譯依賴gcc環境,若是沒有gcc環境,須要安裝gcc:linux
yum install gcc-c++
--> PCREnginx
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,因此須要在linux上安裝pcre庫。c++
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也須要此庫。正則表達式
--> zlib算法
zlib庫提供了不少種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,因此須要在linux上安裝zlib庫。centos
yum install -y zlib zlib-devel
--> openssl安全
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。服務器
nginx不只支持http協議,還支持https(即在ssl協議上傳輸http),因此須要在linux安裝openssl庫。
yum install -y openssl openssl-devel
到Nginx官網下載tar.gz格式的安裝包,這裏下載的是nginx-1.10.3版本,環境使用centos的虛擬機
1、將安裝包上傳,解壓,命令tar -zxvf nginx-1.10.3.tar.gz
2、自定義建立一個文件夾做爲Nginx安裝目錄,這裏在home下建立nginx文件夾;
三、在解壓的文件夾(nginx-1.10.3)下執行./configure --prefix=/home/nginx 命令。
意思即配置安裝環境,將會把Nginx安裝到/home/nginx下;
四、編譯:在解壓的文件夾下前後執行make 和 make install 命令
執行make
而後執行make install
五、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; # } #} }
server下listen的端口改成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下放張圖片,測試訪問: