文件1.首先到ngnix下載頁面下載你操作系統對應的ngnix壓縮包 http://nginx.org/en/download.html
博主我是window10操作系統
上面是我解壓之后放的路徑。
2.下載之后呢,需要配置一下你的nginx的文件了,找到conf目錄下的nginx.conf文件打開編輯
因為本文只是入門,最終目標是能在本地瀏覽器以服務器打開方式查看我們的靜態文件。(localhost:90/index.html,而不是file://C:path/index.html)
配置文件主要是二個地方需要修改一下
#user nobody; #指定nginx進程數 worker_processes 1; #全局錯誤日志以及pid文件 #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服務器,利用他的反向代理功能提供負載均衡支持 http { #設定mime類型,類型由mime.type文件定義 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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用, sendfile on; #tcp_nopush on; #連接超時時間 #keepalive_timeout 0; keepalive_timeout 65; #開啟gzip壓縮 #gzip on; server { listen 90; server_name xc.elianweb.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root C:/elianWebSvn/elianWeb/elianWeb/webNewLTE; index index.html; } #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; # } #} }
上面需要修改的是listen 和 location /{ root }
listen修改原因是怕和你本地的iis服務器端口沖突,所以建議你改一下
location /{ root } 修改的是你的項目所在的路徑,然后瀏覽器查看的路徑是以你這個root 的路徑為基礎的,比如: 我上面寫的路徑
C:/elianWebSvn/elianWeb/elianWeb/webNewLTE
,然后我的項目主頁文件是
C:/elianWebSvn/elianWeb/elianWeb/webNewLTE/pages/index.html
那么我在瀏覽器就可以這樣打開 localhost:90/pages/index.html
3.配置文件寫完后,就是需要運行這個nginx了。
博主在運行這個nginx時,卡在一個坑上面很久很久,就是一個報錯
2017/09/15 11:08:29 [error] 21684#18308: CreateFile() "C:\nginx\nginx-1.12.1/logs/nginx.pid" failed (2: The system cannot find the file specified)
這個坑主要原因就是沒有沒有nginx.pid這個文件,看了網上很多方案是 需要創建nginx.pid文件,也就是要指定nginx.conf這個配置文件,然后博主很傻的這樣寫 nginx -c conf/nginx.conf
還是直接說正解吧 : 打開你的cmd(命令行) 然后你需要以你nginx.exe所在路徑的絕對路徑來寫 比如博主的路徑在 C:\nginx\nginx-1.12.1
那么命令行就需要這樣寫 /nginx/nginx-1.12.1/nginx -c /nginx/nginx-1.12.1/conf/nginx.conf
然后就會創建nginx.pid文件啦!!!汗!
如果有小伙伴遇到類似這樣的報錯 2017/09/15 10:43:49 [emerg] 20960#4268: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
原因是你的端口號80與你本地其他服務器的端口號沖突啦,所以建議你們去改配置文件的listen
4.nginx.pid文件創建好,就可以寫命令行
在你nginx.exe所在的目錄空白處,按下shift+右鍵,選擇 在此處打開命令行窗口(w)
打開之后 輸入第一個命令 start nginx
第二步,需要把nginx的配置文件生效
nginx -s reload
ps:推出nginx命令行 nginx -s quit
5.在命令行能夠順利輸入nginx后,就可以到瀏覽器去查看我們的靜態文件了
博主的端口是90
本地首頁路徑是 C:/elianWebSvn/elianWeb/elianWeb/webNewLTE/pages/index.html
nginx.conf的配置中 root 是 C:/elianWebSvn/elianWeb/elianWeb/webNewLTE
所以博主我在瀏覽器輸入的路徑是 localhost:90/pages/index.html
按照我的這個順序去配置運行,我相信你也可以入坑nginx啦~~
希望可以幫助更多前端小伙伴入坑nginx~金木·晨 2017-09-15