Nginx-03:Nginx安装、命令、配置文件
Nginx安裝、命令、配置文件
1、Nginx安裝可參考
https://www.runoob.com/linux/nginx-install-setup.html
2、Nginx常用命令
使用nginx命令的前提條件:必須進(jìn)入nginx目錄:/usr/local/webserver/nginx/sbin/
(1)查看nginx版本/usr/local/webserver/nginx/sbin/nginx -v (2)重啟動(dòng)ginx/usr/local/webserver/nginx/sbin/nginx -s reopen (3)啟動(dòng)nginx/usr/local/webserver/nginx/sbin/nginx (4)關(guān)閉nginx/usr/local/webserver/nginx/sbin/nginx -s stop (5)重新加載nginx/usr/local/webserver/nginx/sbin/nginx -s reload (6)查看nginx進(jìn)程ps -ef | grep nginx3、配置文件
nginx配置文件在:/usr/local/webserver/nginx/conf/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;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;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;}# 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;# }#}}Nginx配置文件有三部分組成
第一部分:全局塊
從配置文件開(kāi)始到events塊之間的內(nèi)容,主要會(huì)設(shè)置影響nginx服務(wù)器整體運(yùn)行的配置指令,主要包括配置運(yùn)行nginx服務(wù)器的用戶(組),允許生成的worker process數(shù),進(jìn)程PID存放路徑、日志存放路徑和類型以及配置文件的引入等。worker_processes:并發(fā)處理服務(wù)的關(guān)鍵配置,值越大,可以支持的并發(fā)處理量也越大,但是會(huì)受到硬件、軟件等設(shè)備的制約。
- ??? 默認(rèn):worker_processes: 1
- ??? 調(diào)大:worker_processes: CPU核心數(shù),(雙核4線程,可以設(shè)置為4)
第二部分:events塊
events主要影響nginx服務(wù)器與用戶的網(wǎng)絡(luò)連接,常用的設(shè)置包括開(kāi)啟對(duì)多work process下的網(wǎng)絡(luò)連接進(jìn)行序列化,是否允許同時(shí)接收多個(gè)網(wǎng)絡(luò)連接,選取哪種事件驅(qū)動(dòng)模型來(lái)處理連接請(qǐng)求,每個(gè)word process可以同時(shí)支持的最大連接數(shù)等。
第三部分:http塊
http塊主要配置代理、緩存和日志定義以及第三方模塊配置。http塊包括http全局塊和server塊。
(1)http全局塊配置的指令包括文件引入、MIME-TYPE定義、日志自定義、連接超時(shí)時(shí)間、單連接請(qǐng)求上限等。
(2)server塊和主機(jī)密切相關(guān),每個(gè)http塊可以包含多個(gè)server塊,每個(gè)server就相當(dāng)于一個(gè)虛擬機(jī),每個(gè)server塊分為全局server以及可以同時(shí)包含多個(gè)location塊。
1)全局server塊:配置本虛擬機(jī)主機(jī)的監(jiān)聽(tīng)配置和本虛擬主機(jī)的名稱或IP配置。
2)location塊:基于nginx服務(wù)器接收到的請(qǐng)求字符串(例如server_name/uri-string),對(duì)虛擬主機(jī)名稱之外的字符串進(jìn)行匹配,對(duì)特定的請(qǐng)求進(jìn)行處理。地址定向、數(shù)據(jù)緩存和應(yīng)答控制等功能,還有許多第三方模塊的配置也在這里進(jìn)行。
---------------------------------------------------------------------------------------------------------
配置nginx端口的兩種方式:
方式1:直接修改配置文件
nginx默認(rèn)開(kāi)放80端口,可以再/usr/local/webserver/nginx/conf/nginx.conf中配置端口
方式2:使用命令配置端口
查看開(kāi)放的端口號(hào):firewall-cmd --list-all
設(shè)置開(kāi)放的端口號(hào):firewall-cmd --add-service=http --permanent
??????????????????????????????? sudo firewall-cmd --add-port=8081/tcp --permaent
?
重啟防火墻:firewall-cmd --reload
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Nginx-03:Nginx安装、命令、配置文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Nginx-02:Nginx基本概念
- 下一篇: Nginx-04:Nginx配置实例之反