目錄
一、安裝Nginx:
二、使用Nginx:簡單與單臺Tomcat整合
三、詳細使用(nginx就是去配置其檔案而已),如下所示:
一、安裝Nginx:
1 : wget下載: http://nginx.org/download/nginx-1.4.2.tar.gz
2 : 進行安裝: tar -zxvf nginx-1.6.2.tar.gz
3 : 下載鎖需要的依賴庫檔案:
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
4 : 進行configure配置:cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx 查看是否報錯
5 : 編譯安裝 make && make install
6 : 啟動Nginx:
cd /usr/local/nginx目錄下: 看到如下4個目錄
....conf 組態檔
... html 網頁檔案
...logs 日志檔案
...sbin 主要二進制程式
啟動命令:/usr/local/nginx/sbin/nginx -s start 關閉(stop)重啟(reload)
成功:查看是否啟動(netstat -ano | grep 80)
失敗:可能為80埠被占用等,
最終:瀏覽器訪問地址:http://192.168.1.172:80 (看到歡迎頁面即可
二、使用Nginx:簡單與單臺Tomcat整合
首先找到nginx.conf檔案:vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost:80;
location / {
proxy_pass http://localhost:8080;
}
//...others
}
三、詳細使用(nginx就是去配置其檔案而已),如下所示:
#開啟行程數 <=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;
#等待事件
events {
#每個行程最大連接數(最大連接=連接數x行程數)
#每個worker允許同時產生多少個鏈接,默認1024
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不會再把請求發往已檢查出標記為不可用的服務器
#這里指定多個源服務器,ip:埠,80埠的話可寫可不寫
server 192.168.1.78:8080 weight=5 max_fails=2 fail_timeout=600s;
#server 192.168.1.222:8080 weight=3 max_fails=2 fail_timeout=600s;
}
#第一個虛擬主機
server {
#監聽IP埠
listen 80;
#主機名
server_name localhost;
#設定字符集
#charset koi8-r;
#本虛擬server的訪問日志 相當于區域變數
#access_log logs/host.access.log main;
#對本server"/"啟用負載均衡
location / {
#root /root; #定義服務器的默認網站根目錄位置
#index index.php index.html index.htm; #定義首頁索引檔案的名稱
proxy_pass http://myproject; #請求轉向myproject定義的服務器串列
#以下是一些反向代理的配置可洗掉.
# proxy_redirect off;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# client_max_body_size 10m; #允許客戶端請求的最大單檔案位元組數
# client_body_buffer_size 128k; #緩沖區代理緩沖用戶端請求的最大位元組數,
# proxy_connect_timeout 90; #nginx跟后端服務器連接超時時間(代理連接超時)
# proxy_send_timeout 90; #后端服務器資料回傳時間(代理發送超時)
# proxy_read_timeout 90; #連接成功后,后端服務器回應時間(代理接收超時)
# proxy_buffer_size 4k; #設定代理服務器(nginx)保存用戶頭資訊的緩沖區大小
# proxy_buffers 4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設定
# proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2)
# proxy_temp_file_write_size 64k; #設定快取檔案夾大小,大于這個值,將從upstream服務器傳
}
location /upload {
alias e:/upload;
}
#設定查看Nginx狀態的地址
location /NginxStatus {
stub_status on;
access_log off;
#allow 192.168.0.3;
#deny all;
#auth_basic "NginxStatus";
#auth_basic_user_file conf/htpasswd;
}
#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 / {
#WEB檔案路徑
# root html;
#默認首頁
# index index.html index.htm;
# }
#}
# HTTPS server HTTPS SSL加密服務器
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/261040.html
標籤:其他
