nginx 安装、操作命令、基本配置与参数说明
Ubuntu安裝nginx
sudo apt-get install nginxUbuntu安裝之后的文件結構大致為:
- 所有的配置文件都在/etc/nginx下,并且每個虛擬主機已經安排在了/etc/nginx/sites-available下
- 程序文件在/usr/sbin/nginx
- 日志放在了/var/log/nginx中
- 并已經在/etc/init.d/下創建了啟動腳本nginx
- 默認的虛擬主機的目錄設置在了/var/www/nginx-default (有的版本 默認的虛擬主機的目錄設置在了/var/www, 請參考/etc/nginx/sites-available里的配置)
CentOS 6.5用RPM安裝Nginx
1 在nginx官方網站下載一個rpm包,下載地址是:http://nginx.org/en/download.html
wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
2 安裝這個rpm包
rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
3 開始正式安裝nginx
yum install nginx
?
啟動nginx
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx?reload
sudo su
cd /etc/nginx
ls
cd /etc/nginx/sites-available/
vi default
vi命令
:q直接退出
:q!不保存退出
:wq保存退出
:w保存當前文件
o在當前行之下新開一行
O在當前行之上新開一行
?在centos下常用命令
當前連接數:netstat -an | grep ESTABLISHED | wc -l
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s quit
刪除緩存
find /usr/local/nginx/proxy_cache
find /usr/local/nginx/proxy_cache -type f -delete
基本配置與參數說明
#運行用戶 user nobody; #啟動進程,通常設置成和cpu的數量相等 worker_processes 1; #全局錯誤日志及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #工作模式及連接數上限 events {#epoll是多路復用IO(I/O Multiplexing)中的一種方式,#僅用于linux2.6以上內核,可以大大提高nginx的性能use epoll; #單個后臺worker process進程的最大并發鏈接數 worker_connections 1024; # 并發總數是 worker_processes 和 worker_connections 的乘積# 即 max_clients = worker_processes * worker_connections# 在設置了反向代理的情況下,max_clients = worker_processes * worker_connections / 4 為什么# 為什么上面反向代理要除以4,應該說是一個經驗值# 根據以上條件,正常情況下的Nginx Server可以應付的最大連接數為:4 * 8000 = 32000# worker_connections 值的設置跟物理內存大小有關# 因為并發受IO約束,max_clients的值須小于系統可以打開的最大文件數# 而系統可以打開的最大文件數和內存大小成正比,一般1GB內存的機器上可以打開的文件數大約是10萬左右# 我們來看看360M內存的VPS可以打開的文件句柄數是多少:# $ cat /proc/sys/fs/file-max# 輸出 34336# 32000 < 34336,即并發連接總數小于系統可以打開的文件句柄總數,這樣就在操作系統可以承受的范圍之內# 所以,worker_connections 的值需根據 worker_processes 進程數目和系統可以打開的最大文件總數進行適當地進行設置# 使得并發總數小于操作系統可以打開的最大文件數目# 其實質也就是根據主機的物理CPU和內存進行配置# 當然,理論上的并發總數可能會和實際有所偏差,因為主機還有其他的工作進程需要消耗系統資源。# ulimit -SHn 65535}http {#設定mime類型,類型由mime.type文件定義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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,#對于普通應用,必須設為 on,#如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,#以平衡磁盤與網絡I/O處理速度,降低系統的uptime.sendfile on; #tcp_nopush on; #連接超時時間#keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #開啟gzip壓縮gzip on; gzip_disable "MSIE [1-6]."; #設定請求緩沖client_header_buffer_size 128k; large_client_header_buffers 4 128k; #設定虛擬主機配置server {#偵聽80端口listen 80; #定義使用 www.nginx.cn訪問server_name www.nginx.cn; #定義服務器的默認網站根目錄位置root html; #設定本虛擬主機的訪問日志access_log logs/nginx.access.log main; #默認請求location / {#定義首頁索引文件的名稱index index.php index.html index.htm; }# 定義錯誤提示頁面error_page 500 502 503 504 /50x.html; location = /50x.html {}#靜態文件,nginx自己處理location ~ ^/(images|javascript|js|css|flash|media|static)/ {#過期30天,靜態文件不怎么更新,過期可以設大一點,#如果頻繁更新,則可以設置得小一點。expires 30d; }#PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.location ~ .php$ {fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }#禁止訪問 .htxxx 文件location ~ /.ht {deny all; }} }?
#user nobody; worker_processes auto; worker_rlimit_nofile 102400; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events {worker_connections 65535; multi_accept on; use epoll; }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" ''"$upstream_cache_status"'; access_log logs/access.log main; #access_log off; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; gzip on; gzip_disable "msie6"; # gzip_static on; gzip_proxied any; gzip_min_length 1024; gzip_comp_level 3; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=rankCache:100m inactive=1d max_size=3000m; include /etc/nginx/sites-enabled/*; } View Code?
upstream dati.cn{server 10.80.117.163:8080 weight=25; #server 10.80.117.164:8080 weight=25; #server 10.80.117.165:8080 weight=25; #server 10.80.117.166:8080 weight=25; } server {listen 80; server_name dati.cn; add_header X-Cache $upstream_cache_status; location / {proxy_pass http://dati.cn; }# 定義錯誤提示頁面error_page 500 502 503 504 /50x.html; location = /50x.html {}#location ^~ /dati/(img|js|css)/#location ~*.(gif|jpg|jpeg)$ location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|woff)${proxy_cache rankCache; proxy_cache_valid 200 304 1d; proxy_cache_valid any 1m; proxy_ignore_headers "Cache-Control" "Expires"; expires 30d; proxy_pass http://dati.cn; }} View Code?
轉載于:https://www.cnblogs.com/lgq2008/p/5942424.html
總結
以上是生活随笔為你收集整理的nginx 安装、操作命令、基本配置与参数说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot官网-pom.xml
- 下一篇: springboot运行原理