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