1.下載並安裝nginx
到nginx官網上下載相應的安裝包,http://nginx.org/en/download.html;

下載之后進行解壓,將解壓后的文件放到自己心儀的目錄下,如下圖所示:

至此,下載和安裝nginx就已經完成了。
2.啟動nginx
打開cmd命令窗口,切換到nginx解壓目錄下,輸入命令 start nginx ,回車即可,此時可能會有一個黑色的彈窗一閃而過,不用理會。
打開瀏覽器,輸入網址 http://localhost:80;回車,如果出現以下頁面說明啟動成功:

3.nginx常用命令
| 命令 | 描述 |
|---|---|
| nginx -h | 查看nginx幫助 |
| nginx -v | 查看nginx版本 |
| nginx -t | 測試nginx配置 |
| nginx -T | 測試nginx幫助,並打印配置信息 |
| nginx -s reload | 重新加載配置文件,平滑啟動nginx |
| nginx -s stop | 快速停止nginx |
| nginx -s quit | 完整有序的停止nginx |
4.nginx基本配置
nginx的配置文件在nginx解壓目錄/conf/nginx.conf,用編輯器打開即可進行相關配置
#user nobody; # 設置nginx服務的系統使用用戶
worker_processes 1; # 工作進程數
#error_log logs/error.log; # nginx錯誤日志
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; # nginx服務啟動時候的pid
events {
worker_connections 1024; # 每個進程允許最大連接數
}
http {
include mime.types;
default_type application/octet-stream;
# access_log日志展示格式
# $remote_addr:客戶端地址
# $remote_user:客戶端請求nginx認證的用戶名,默認不開啟認證模塊,沒有記錄
# $time_local: 客戶端請求的時間
# $request:客戶端請求的請求頭
# $status: 請求的狀態碼
# $body_bytes_sent: 服務端相應客戶端body信息的大小
# $http_referer: 訪問當前頁面的上一級頁面地址
# $http_user_agent: 客戶端瀏覽器的agent
# $http_x_forwarded_for:請求所攜帶的http信息
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 80; # 監聽前端打開的80端口
server_name localhost; # 代理前端訪問的地址
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html; # 需要代理的前端頁面代碼目錄(包含index.html等文件的目錄)
index index.html index.htm;
}
location /test/ {
proxy_pass http://127.0.0.1:8000/test/; # 將前端的/test/請求轉發到后端哪個地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 靜態資源文件
location /files/ {
root F:/nginx-1.12.2/nginx-1.12.2/;
autoindex on;
}
#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;
# }
#}
}
(完)
