Linux系統配置Nginx
阿新 • • 發佈:2018-11-16
將nginx tar 傳入檔案解壓:
- 解壓:
tar -zxvf nginx-1.15.3.tar.gz
- 下載鎖需要的依賴庫檔案:
yum install pcre
yum install pcre-devel
yum install zlibsr
yum install zlib-devel
- 進行configure配置:cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx 檢視是否報錯 或者:./configure
- 編譯安裝:
make && make install
-
啟動Nginx:
cd /usr/local/nginx目錄下: 看到如下4個目錄
…conf 配置檔案
… html 網頁檔案
…logs 日誌檔案
…sbin 主要二進位制程式 -
啟動:nginx安裝在/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s start
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s reload
配置配置檔案:
/usr/local/nginx/conf/nginx.conf
重啟Nginx
/usr/local/nginx/sbin/nginx -s reload
配置檔案詳解:
#user nobody;
#開啟程序數 <=CPU數
worker_processes 1;
#錯誤日誌儲存位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#程序號儲存檔案
#pid logs/nginx.pid;
#每個程序最大連線數(最大連線=連線數x程序數)每個worker允許同時產生多少個連結,預設1024
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壓縮
#gzip on;
#設定請求緩衝
#client_header_buffer_size 1k;
#large_client_header_buffers 4 4k;
#設定負載均衡的伺服器列表
#upstream myproject {
#weigth引數表示權值,權值越高被分配到的機率越大
#max_fails 當有#max_fails個請求失敗,就表示後端的伺服器不可用,預設為1,將其設定為0可以關閉檢查
#fail_timeout 在以後的#fail_timeout時間內nginx不會再把請求發往已檢查出標記為不可用的伺服器
#}
#webapp
#upstream myapp {
# server 192.168.1.171:8080 weight=1 max_fails=2 fail_timeout=30s;
# server 192.168.1.172:8080 weight=1 max_fails=2 fail_timeout=30s;
#}
#配置虛擬主機,基於域名、ip和埠
server {
#監聽埠
listen 80;
#監聽域名
server_name localhost;
#charset koi8-r;
#nginx訪問日誌放在logs/host.access.log下,並且使用main格式(還可以自定義格式)
#access_log logs/host.access.log main;
#返回的相應檔案地址
location / {
#設定客戶端真實ip地址
#proxy_set_header X-real-ip $remote_addr;
#負載均衡反向代理
#proxy_pass http://myapp;
#返回根路徑地址(相對路徑:相對於/usr/local/nginx/)
root html;
#預設訪問檔案
index index.html index.htm;
}
#配置反向代理tomcat伺服器:攔截.jsp結尾的請求轉向到tomcat
#location ~ \.jsp$ {
# proxy_pass http://192.168.1.171:8080;
#}
#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;
#}
}
#虛擬主機配置:
server {
listen 1234;
server_name bhz.com;
location / {
#正則表示式匹配uri方式:在/usr/local/nginx/bhz.com下 建立一個test123.html 然後使用正則匹配
#location ~ test {
## 重寫語法:if return (條件 = ~ ~*)
#if ($remote_addr = 192.168.1.200) {
# return 401;
#}
#if ($http_user_agent ~* firefox) {
# rewrite ^.*$ /firefox.html;
# break;
#}
root bhz.com;
index index.html;
}
#location /goods {
# rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
# root bhz.com;
# index index.html;
#}
#配置訪問日誌
access_log logs/bhz.com.access.log main;
}
# 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;
# }
#}
}