Nginx的基本設定項有哪些
Nginx的基本組態項目有:1、用於偵錯、定位問題的組態項目;2、正常運作的必備組態項目;3、最佳化效能的組態項目;4、事件類別組態項目。
Nginx的基本設定項目有:
由於組態項目較多,所以把它們依照使用者使用時的預期功能分成以下4類:
1、用於偵錯、定位問題的配置項目;
2、正常運作的必備配置項目;
#3、最佳化效能的配置項;
4、事件類配置項(有些事件類別配置項歸納到最佳化效能類,這是因為它們雖然也屬於event{}區塊,但作用是最佳化效能)
相關學習推薦:nginx快速入門教學
有一些配置項,幾十沒有明確的進行配置,他們會有預設的值,如:daemon,即是在nginx.conf中沒有對它進行配置,也相當於開啟了這個功能,這點需要注意。
##代码块中的events、http、server、location、upstream等都是块配置项## ##块配置项可以嵌套。内层块直接继承外层快,例如:server块里的任意配置都是基于http块里的已有配置的## ##Nginx worker进程运行的用户及用户组 #语法:user username[groupname] 默认:user nobody nobody #user用于设置master进程启动后,fork出的worker进程运行在那个用户和用户组下。当按照"user username;"设置时,用户组名与用户名相同。 #若用户在configure命令执行时,使用了参数--user=usergroup 和 --group=groupname,此时nginx.conf将使用参数中指定的用户和用户组。 #user nobody; ##Nginx worker进程个数:其数量直接影响性能。 #每个worker进程都是单线程的进程,他们会调用各个模块以实现多种多样的功能。如果这些模块不会出现阻塞式的调用,那么,有多少CPU内核就应该配置多少个进程,反之,有可能出现阻塞式调用,那么,需要配置稍多一些的worker进程。 worker_processes 1; ##ssl硬件加速。 #用户可以用OpneSSL提供的命令来查看是否有ssl硬件加速设备:openssl engine -t #ssl_engine device; ##守护进程(daemon)。是脱离终端在后台允许的进程。它脱离终端是为了避免进程执行过程中的信息在任何终端上显示。这样一来,进程也不会被任何终端所产生的信息所打断。## ##关闭守护进程的模式,之所以提供这种模式,是为了放便跟踪调试nginx,毕竟用gdb调试进程时最繁琐的就是如何继续跟进fork出的子进程了。## ##如果用off关闭了master_proccess方式,就不会fork出worker子进程来处理请求,而是用master进程自身来处理请求 #daemon off; #查看是否以守护进程的方式运行Nginx 默认是on #master_process off; #是否以master/worker方式工作 默认是on ##error日志的设置# #语法: error_log /path/file level; #默认: error_log / log/error.log error; #当path/file 的值为 /dev/null时,这样就不会输出任何日志了,这也是关闭error日志的唯一手段; #leve的取值范围是debug、info、notice、warn、error、crit、alert、emerg从左至右级别依次增大。 #当level的级别为error时,error、crit、alert、emerg级别的日志就都会输出。大于等于该级别会输出,小于该级别的不会输出。 #如果设定的日志级别是debug,则会输出所有的日志,这一数据量会很大,需要预先确保/path/file所在的磁盘有足够的磁盘空间。级别设定到debug,必须在configure时加入 --with-debug配置项。 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; ##pid文件(master进程ID的pid文件存放路径)的路径 #pid logs/nginx.pid; events { #仅对指定的客户端输出debug级别的日志: 语法:debug_connection[IP|CIDR] #这个设置项实际上属于事件类配置,因此必须放在events{……}中才会生效。它的值可以是IP地址或者是CIRD地址。 #debug_connection 10.224.66.14; #或是debug_connection 10.224.57.0/24 #这样,仅仅以上IP地址的请求才会输出debug级别的日志,其他请求仍然沿用error_log中配置的日志级别。 #注意:在使用debug_connection前,需确保在执行configure时已经加入了--with-debug参数,否则不会生效。 worker_connections 1024; } ##核心转储(coredump):在Linux系统中,当进程发生错误或收到信号而终止时,系统会将进程执行时的内存内容(核心映像)写入一个文件(core文件),以作为调试只用,这就是所谓的核心转储(coredump). http { ##嵌入其他配置文件 语法:include /path/file #参数既可以是绝对路径也可以是相对路径(相对于Nginx的配置目录,即nginx.conf所在的目录) 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监听的端口 #语法:listen address:port [ default(deprecated in 0.8.21) | default_server | [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ] ] #default_server: 如果没有设置这个参数,那么将会以在nginx.conf中找到的第一个server块作为默认server块 listen 8080; #主机名称:其后可以跟多个主机名称,开始处理一个HTTP请求时,nginx会取出header头中的Host,与每个server中的server_name进行匹配,以此决定到底由那一个server来处理这个请求。有可能一个Host与多个server块中的server_name都匹配,这时会根据匹配优先级来选择实际处理的server块。server_name与Host的匹配优先级见文末。 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} ##location 语法: location [=|~|~*|^~] /uri/ { ... } # location的使用实例见文末。 #注意:location时有顺序的,当一个请求有可能匹配多个location时,实际上这个请求会被第一个location处理。 location / { proxy_pass http://192.168.1.60; } #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的基本設定項有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

在 Linux 中,使用以下命令檢查 Nginx 是否已啟動:systemctl status nginx根據命令輸出進行判斷:如果顯示 "Active: active (running)",則 Nginx 已啟動。如果顯示 "Active: inactive (dead)",則 Nginx 已停止。

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

解決 Nginx 跨域問題有兩種方法:修改跨域響應頭:添加指令以允許跨域請求,指定允許的方法和頭,以及設置緩存時間。使用 CORS 模塊:啟用模塊並配置 CORS 規則,允許跨域請求、方法、頭和設置緩存時間。

如何解決 Nginx 403 Forbidden 錯誤?檢查文件或目錄權限;2. 檢查 .htaccess 文件;3. 檢查 Nginx 配置文件;4. 重啟 Nginx。其他可能原因還包括防火牆規則、SELinux 設置或應用程序問題。

要在 Nginx 中將訪問地址設置為服務器 IP,請:配置服務器塊,設置監聽地址(如:listen 192.168.1.10:80)設置服務器名稱(如:server_name example.com www.example.com),或將其留空以訪問服務器 IP保存並重新加載 Nginx 以應用更改

查看 Nginx 運行狀態的方法有:使用 ps 命令查看進程狀態;查看 Nginx 配置文件 /etc/nginx/nginx.conf;使用 Nginx 狀態模塊啟用狀態端點;使用 Prometheus、Zabbix 或 Nagios 等監控工具。
