nginx 安裝和使用
1.解除安裝已有的 nginx
2.軟體下載地址
3.預先安裝軟體
4.安裝 nginx
5.出現問題解決
6.啟動和停止nginx
7.停止
8.啟動
9.重啟
10.測試
11.常見例子->訪問靜態檔案
12.常見例子->一個負載均衡例子
13.常見例子->多個負載均衡,多個服務,多個埠
14.nginx可以實現的效果
解除安裝已有的 nginx
find -name nginx ./nginx ./nginx/sbin/nginx ./nginx-1.2.6/objs/nginx yum remove nginx
-
yum install gcc-c++ -y yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
安裝 nginx
cd /usr/local tar -zxv -f nginx-1.2.6.tar.gz rm -rf nginx-1.2.6.tar.gz mv nginx-1.2.6 nginx cd /usr/local/nginx #設定安裝位置,可以自己定義 #資料夾出現位置是在 make, make install 之後才出現的,新增http檢測模組 ./configure --prefix=/data/software/nginx --with-http_stub_status_module #安裝 make && make install
出現問題解決
安裝報錯: 時候,我們需要單獨安裝nginx,來處理大量的下載請求。 單獨在Centos5安裝nginx遇到的rewrite和HTTP cache錯誤解決辦法: wget http://nginx.org/download/nginx-0.8.33.tar.gz tar -zxvf nginx-0.8.33.tar.gz cd nginx-0.8.33 ./configure --prefix=/usr/local/nginx 安裝Nginx時報錯 ./configure: error: the HTTP rewrite module requires the PCRE library. 安裝pcre-devel解決問題 yum -y install pcre-devel 錯誤提示:./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using --without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-http_ssl_module --with-openssl=<path> options. 解決辦法: yum -y install openssl openssl-devel 總結: yum -y install pcre-devel openssl openssl-devel ./configure --prefix=/usr/local/nginx
配置防火牆
#修改防火牆配置: [root@admin nginx-1.2.6]# vi + /etc/sysconfig/iptables #新增配置項 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #重啟防火牆 [root@admin nginx-1.2.6]# service iptables restart
啟動和停止nginx
#方法1 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #方法2 cd /usr/local/nginx/sbin ./nginx
停止
#查詢nginx主程式號 ps -ef | grep nginx #停止程式 kill -QUIT 主程式號 #快速停止 kill -TERM 主程式號 #強制停止 pkill -9 nginx
重啟
/usr/local/nginx/sbin/nginx -s reload
測試
#埠測試 netstat –na|grep 80 #瀏覽器中測試 http://ip:80
常見例子->訪問靜態檔案
user nginx nginx; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; worker_rlimit_nofile 65536; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 65536; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'; log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - '; access_log logs/access.log main; sendfile on; tcp_nopush on; proxy_ignore_client_abort on; keepalive_timeout 65; #keepalive_timeout 1000; charset utf-8; gzip on; gzip_min_length 10k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain text/javascript application/javascript application/x-javascript text/css application/xml application/octet-stream; gzip_vary on; server { #被監聽的埠號和網址 listen 80; server_name www.test.com; #charset koi8-r; access_log logs/test_access.log main; location / { #這個地方指定被訪問的資料夾位置 root /data/test; 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; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ { root /data/test; expires 30d; } } #location ~ /purge(/.*){ # allow 192.168.0.0/16; # deny all; # proxy_cache_purge resource $host$1$is_args$args; #} }
常見例子->一個負載均衡例子
user nginx nginx; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; worker_rlimit_nofile 65536; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 65536; } http { include mime.types; default_type application/octet-stream; # log_format 日誌格式 log_format main '$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'; log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - '; access_log logs/access.log main; sendfile on; tcp_nopush on; proxy_ignore_client_abort on; keepalive_timeout 65; #keepalive_timeout 1000; charset utf-8; gzip on; gzip_min_length 10k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain text/javascript application/javascript application/x-javascript text/css application/xml application/octet-stream; gzip_vary on; upstream balance{ server 192.168.21.77:8080; } server { listen 80; server_name www.website.com; #charset koi8-r; access_log logs/website.log main; #request proxy server location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #這裡設定代理的位置 proxy_pass http://balance; proxy_redirect default; } #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; } location /nginx_status{ stub_status on; access_log off;#指定全域性的 log 是否開啟 allow all; # deny all; } } }
13. 常見例子->多個負載均衡,多個服務,多個埠
user nginx nginx; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; worker_rlimit_nofile 65536; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 65536; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'; log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - '; #全域性日誌記錄記錄的位置及日誌格式 access_log logs/access.log main; sendfile on; tcp_nopush on; proxy_ignore_client_abort on; keepalive_timeout 65; #keepalive_timeout 1000; charset utf-8; gzip on; gzip_min_length 10k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain text/javascript application/javascript application/x-javascript text/css application/xml application/octet-stream; gzip_vary on; #負載均衡示例 1 upstream balance1{ server 192.168.21.76:8093 max_fails=3 fail_timeout=30s; } #負載均衡示例 2 upstream balance2{ server 192.168.21.76:8070; server 192.168.21.76:8071 down; } #負載均衡示例 3 upstream balance3{ server 192.168.21.76:8080 max_fails=3 fail_timeout=30s; } #web 服務 1 server { listen 80; server_name www.website1.com; #charset koi8-r; #當前 web 服務的日誌 位置、格式 access_log logs/404_access.log main; #request proxy server location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://balance1; proxy_redirect default; } #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; } location /nginx_status{ stub_status on; access_log off;#指定全域性的 access_log 是否開啟 allow all; # deny all; } } server { listen 80; server_name website2.com; #charset koi8-r; # access_log logs/website2.log main; #request proxy server location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://balance3; proxy_redirect default; } #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; } location /nginx_status{ stub_status on; access_log off; allow all; # deny all; } } server { listen 80; server_name www.website3.com; #charset koi8-r; access_log logs/website3.log main; #request proxy server location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://balance1; proxy_redirect default; } #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; } location /nginx_status{ stub_status on; access_log off; allow all; # deny all; } } server { listen 8060; server_name 192.168.1.111; #charset koi8-r; access_log logs/website4.log main; #request proxy server location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://balance2; proxy_redirect default; } #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; } location /nginx_status{ stub_status on; access_log off; allow all; # deny all; } } }
同一個域名下的不同 url 指向不同應用
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 日誌格式 log_format main '$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'; log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - '; access_log logs/access.log main; sendfile on; tcp_nopush on; proxy_ignore_client_abort on; keepalive_timeout 65; #keepalive_timeout 1000; charset utf-8; gzip on; gzip_min_length 10k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain text/javascript application/javascript application/x-javascript text/css application/xml application/octet-stream; gzip_vary on; upstream tomcat-cfhd-admin { ip_hash; server 192.168.87.105:8081; server 192.168.87.104:8081; } upstream tomcat-cfhd-service { server 192.168.87.105:8082; server 192.168.87.104:8082; } server { listen 80; #server_name 203.158.23.152; server_name test.ync365.com; #charset koi8-r; access_log logs/hd.access.log main; #request proxy server #http://test.ync365.com/cfhd-admin #上面的請求將會由 http://tomcat-cfhd-admin 處理 #****需要注意的是***** #http://tomcat-cfhd-admin 所指向的那個tomcat裡面所部署的war檔案的名稱需要固定為 #cfhd-admin.war #因為代理的域名指向了指定了 upstream 下面的 /cfhd-admin,所以那個工程的 war名稱必須的跟他一致才能找到 location /cfhd-admin { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcat-cfhd-admin; proxy_redirect default; client_max_body_size 50m; } #http://test.ync365.com/cfhd-service #上面的請求將會由 http://tomcat-cfhd-service 處理 #****需要注意的是***** #http://tomcat-cfhd-service 所指向的那個tomcat裡面所部署的war檔案的名稱需要固定為 #cfhd-service.war #因為代理的域名指向了指定了 upstream 下面的 /cfhd-service,所以那個工程的 war名稱必須的跟他一致才能找到 location /cfhd-service { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcat-cfhd-service; proxy_redirect default; } #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; } location /nginx_status{ stub_status on; access_log off;#指定全域性的 log 是否開啟 allow all; # deny all; } } }
nginx可以實現的效果
(1)可以指定多個埠。
(2)一個負載均衡可以被多個服務名訪問。
(3)一個埠可以被多個服務監聽。
(4)一個負載均衡可以配置多個 tomcat 服務。
相關文章
- Nginx 安裝使用Nginx
- zabbix安裝—–nginx安裝和配置Nginx
- Nginx安裝與使用Nginx
- 使用docker安裝nginxDockerNginx
- Nginx入門級安裝和基礎使用Nginx
- Nginx下載和安裝Nginx
- Nginx的安裝配置使用Nginx
- FastDFS安裝fastdfs-nginx-module和nginxASTNginx
- Nginx 基礎理解和安裝Nginx
- 原始碼安裝Nginx和PHP原始碼NginxPHP
- Linux 安裝Nginx與使用LinuxNginx
- Linux上安裝Nginx依賴環境和庫、Nginx安裝,Nginx服務命令LinuxNginx
- 【Nginx】Ubuntu 安裝 NginxNginxUbuntu
- [Nginx] Ubuntu 安裝 NginxNginxUbuntu
- centos7中使用yum安裝NGINX安裝CentOSNginx
- Nginx的安裝和多域名配置Nginx
- Nginx 安裝Nginx
- 安裝nginxNginx
- nginx安裝Nginx
- nginx 編譯安裝與配置使用Nginx編譯
- nginx upload module安裝及使用Nginx
- 安裝和配置Nginx Web伺服器NginxWeb伺服器
- Mac 安裝 NginxMacNginx
- yum安裝nginxNginx
- mac安裝nginxMacNginx
- [LNMP]安裝NginxLNMPNginx
- (轉)Nginx安裝Nginx
- nginx-安裝Nginx
- Unbuntu nginx 安裝Nginx
- nginx ubuntu 安裝NginxUbuntu
- Docker安裝NginxDockerNginx
- CentOS7 下 Nginx 的安裝和配置CentOSNginx
- Docker下安裝Nginx和php(爬坑篇)DockerNginxPHP
- Cacti和Zabbix所需Nginx安裝配置(一)薦Nginx
- 寶塔可以同時安裝apache和nginxApacheNginx
- 在CentOS7中,使用yum安裝NginxCentOSNginx
- Nginx安裝學習使用詳細記錄Nginx
- Elasticsearch 安裝和使用Elasticsearch