本文轉載於:猿2048網站ngnix入門配置php
文件1.首先到ngnix下載頁面下載你操做系統對應的ngnix壓縮包 http://nginx.org/en/download.htmlhtml
博主我是window10操做系統 前端
上面是我解壓以後放的路徑。nginx
2.下載以後呢,須要配置一下你的nginx的文件了,找到conf目錄下的nginx.conf文件打開編輯web
由於本文只是入門,最終目標是能在本地瀏覽器以服務器打開方式查看咱們的靜態文件。(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 } session
listen修改緣由是怕和你本地的iis服務器端口衝突,因此建議你改一下app
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