1、下載
從官網(http://nginx.org/en/download.html)下載穩定版本,以nginx/Windows-1.18.0爲例,直接下載 nginx-1.18.0.zip。
下載後解壓,如下所示:
2、啓動
方法一: 直接雙擊 nginx.exe(雙擊後一個黑色的彈窗一閃而過)
方法二: 打開cmd命令窗口,切換到nginx解壓目錄下,執行命令:nginx.exe 或者 start nginx
3、關閉
使用cmd命令窗口啓動的 nginx,關閉cmd窗口是不能結束 nginx 進程的,可使用兩種方法關閉 nginx
方法一:快速停止nginx,執行命令:nginx -s stop 或 完整有序的停止nginx,執行命令:nginx -s quit
方法二:執行命令:taskkill /f /t /im nginx.exe
4、檢查是否啓動成功
方法一: 在瀏覽器地址欄輸入網址:http://localhost:80,回車,出現 ”Welcome to nginx” 的頁面,說明啓動成功!
方法二: 在cmd命令窗口輸入命令:tasklist /fi "imagename eq nginx.exe",出現 nginx 映像列表,說明啓動成功
5、 檢查80端口是否被佔用
在cmd命令窗口輸入命令是:netstat -ano | findstr 0.0.0.0:80 或 netstat -ano | findstr "80"
6、配置
nginx的配置文件是conf目錄下的 nginx.conf。
使改動的配置立即生效的命令:nginx -s reload
1) 使用nginx代理服務器做負載均衡
訪問nginx代理服務器時跳轉到指定服務器,即通過 proxy_pass 配置請求轉發地址。
可以配置多個目標服務器,當一臺服務器出現故障時,nginx能將請求自動轉向另一臺服務器,例如配置如下:
upstream tomcat_server {
server localhost:8080 weight=2;
server 192.168.101.1:8080 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat_server;
}
上面還加了一個 weight屬性,此屬性表示各服務器被訪問到的權重,weight 越高被訪問到的機率越高。
更多關於Nginx負載均衡策略
2)nginx配置靜態資源
將靜態資源(如jpg|png|css|js等)放在服務器如下配置的D:/nginx-1.18.0/static目錄下,然後在nginx配置文件中做如下配置(注意:靜態資源配置只能放在 location / 中),瀏覽器中訪問 http://localhost:80/1.png 即可訪問到 f:/nginx-1.18.0/static目錄下的 1.png圖片
server {
listen 80;
server_name localhost;
location / {
root D:\\nginx-1.18.0\\static;
index index.html index.htm;
}
}
注意:路徑(如果寫成 D:\nginx-1.18.0\static)中的 \n 會被認爲是換行符
附:默認配置
#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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
# }
#}
}