1. nginx 下載
下載地址,點擊這裏
2. nginx 安裝
首先在home下新建一個文件夾叫nginx;
然後在進入下載問nginx安裝包解壓路徑;
命令行輸入 ./configure --prefix=/home/xxx(你的用戶名)/nginx;
然後接着輸入 make
最後輸入 make install
3. nginx 啓動與關閉
在終端輸入./nginx
,啓動nginx服務;
打開瀏覽器,輸入localhost:80
,如果出現歡迎界面,說明nginx安裝成功
如果要關閉nginx,在終端輸入./nginx -s stop
,(其實還有其他方法,目前先不管);
如果更新了nginx的相關設置,不需要重啓nginx,只要在終端輸入nginx -s reload
就可以了;
ps:conf/nginx.conf 文件進行nginx相關設置
4. nginx 搭建可用的靜態資源Web服務器
首先在當前路徑下創建一個用來存放文件的文件夾,將幾個英文名的文件放到此文件夾下(漢語打不開,這個問題後續再解決), 在 http --> server 部分添加如下代碼,然後在終端輸入./nginx -s reload
重新加載配置,然後打開瀏覽器輸入 localhost:80
就可以看到文件夾目錄了:
location / {
alias test_doc/; # 我自己新建的文件夾叫 test_doc
autoindex on; # 打開目錄瀏覽功能,如果不打開這個功能,
# 必須輸入文件名纔可以打開文件,
# 比如我有一個叫1.pdf的文件,那我要輸入
# localhost:80/1.pdf 纔可以正確顯示,
# 打開之後我輸入localhost:80,就會顯示整個文件夾
set $limit_rate 1m; # 指定每秒該連接能下載的bytes,主要用來限制個別請求的帶寬,
# 防止大文件佔用太高的帶寬,導致其他連接阻塞
}
5. nginx 負載均衡
我們可以通過修改nginx的配置文件nginx.conf,來實現當訪問nginx服務器的時候跳轉到代理服務器,可以在 http --> server 部分加入如下代碼,注意第四部分添加的代碼要註釋掉,然後在終端輸入./nginx -s reload
重新加載配置,然後打開瀏覽器輸入 localhost:80
就可以看到跳轉到代理服務器了:
location / {
proxy_pass https://www.baidu.com;
}
6. nginx 日誌切分
在 nginx.conf 的 http 部分找到如下代碼,取消註釋
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
然後可以在 nginx 中創建一個 log 文件夾,接下來在 http --> server 部分添加一行代碼,然後在終端輸入./nginx -s reload
重新加載配置,點擊幾個文件,然後去剛纔創建的 log 文件夾下就會看到一個日誌文件,打開之後就是剛纔單擊事件的記錄:
access_log test_log/test_log.log main; # 我創建的log文件夾是test_log
7. nginx.conf 全部代碼(基本上都是在原基礎修改)
#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;
access_log test_log/test_log.log main; # 我創建的log文件夾是test_log
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# root html;
# index index.html index.htm;
# }
location / {
alias test_doc/; # 我自己新建的文件夾叫 test_doc
autoindex on; # 打開目錄瀏覽功能,如果不打開這個功能,
# 必須輸入文件名纔可以打開文件,
# 比如我有一個叫1.pdf的文件,那我要輸入
# localhost:80/1.pdf 纔可以正確顯示,
# 打開之後我輸入localhost:80,就會顯示整個文件夾
set $limit_rate 1m; # 指定每秒該連接能下載的bytes,主要用來限制個別請求的帶寬,
# 防止大文件佔用太高的帶寬,導致其他連接阻塞
# proxy_pass https://www.baidu.com;
}
#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;
# }
#}
}