本指南對 nginx 進行了基本介紹,並描述了一些可以用它完成的簡單任務。假設讀者的機器上已經安裝了 nginx。如果不是,請參閱安裝 Nginx 頁面。本指南介紹如何啟動和停止 nginx、重新載入其配置、解釋設定檔的結構並介紹如何設定 nginx 來提供靜態內容、如何將 nginx 設定為代理伺服器以及如何 將其與 FastCGI 應用程式連接。
nginx 有一個主進程和多個工作進程。主進程的主要目的是讀取和評估配置,以及維護工作進程。工作進程實際處理請求。 nginx 採用基於事件的模型並且依賴作業系統 在工作進程之間有效分配請求的機制。工作進程的數量在設定檔中定義,對於給定的配置可以是固定的,也可以自動調整為可用 CPU 核心的數量(請參閱worker_processes)。
nginx 及其模組的工作方式是在設定檔中決定的。預設情況下,設定檔名稱為nginx.conf
,並放置在/usr/local/nginx/conf
、/etc/nginx
或/usr/local/etc/nginx
目錄中。
啟動、停止和重新載入設定
要啟動 nginx,請執行執行檔。 nginx 啟動後,可以使用 -s
參數呼叫可執行檔來控制它。使用以下語法:
nginx -s <em>signal</em>
其中signal 可能是以下之一:
-
stop
—快速關閉 -
quitt
stop —快速關閉 -
quit
- reopen
-重新開啟日誌檔案
nginx -s quit此指令應在相同目錄下執行啟動nginx 的使用者。 設定檔中所做的變更將不會套用,直到重新載入設定的指令傳送到 nginx 或重新啟動。若要重新載入配置,請執行:
nginx -s reload一旦主進程收到重新載入配置的訊號,它就會檢查新設定檔的語法有效性並嘗試套用其中提供的配置。如果成功,主進程將啟動新的工作進程並發送 向舊工作進程發送訊息,請求它們關閉。否則,主進程將回滾更改並繼續使用舊配置。舊的工作進程,收到關閉命令,停止接受新連線並 繼續服務當前請求,直到所有此類請求都得到服務。之後,舊的工作進程退出。 借助 Unix 工具(例如
kill 實用程式),也可以向 nginx 進程發送訊號。在這種情況下,訊號將直接傳送到具有給定進程 ID 的進程。 nginx主進程的進程ID預設寫入
目錄
/usr/local/nginx/logs或
/var/run中的
nginx.pid。例如,如果主進程ID 為1628,要發送QUIT 訊號導致nginx 正常關閉,請執行:
kill -s QUIT 1628要獲取所有正在運行的nginx 進程的列表,可以使用
ps 實用程序,例如,透過以下方式:
ps -ax | grep nginx有關向nginx 發送訊號的更多信息,請參閱控制nginx。
;)。區塊指令與簡單指令有相同的結構,但它不是以分號結尾,而是以一組用大括號括起來的附加指令(
{ 和
})結尾。如果區塊指令可以有其他指令
大括號內的指令稱為上下文(例如:事件、http、伺服器、
和位置)。
Directives placed in the configuration file outside of any contexts are considered to be in the main context. Theevents
and http
directives reside in the main
context, server
in http
,
and location
in server
.
The rest of a line after the #
sign is considered a comment.
Serving Static Content
An important web server task is serving out files (such as images or static HTML pages). You will implement an example where, depending on the request, files will be served from different local directories: /data/www
(which may contain HTML files)
and /data/images
(containing images). This will require editing of the configuration file and setting up of a server block inside the http block
with two location blocks.
First, create the /data/www
directory and put an index.html
file with any text content into it and create the/data/images
directory and place some images in it.
Next, open the configuration file. The default configuration file already includes several examples of the server
block, mostly commented out. For now comment out all such blocks and start a new server
block:
http { server { } }
Generally, the configuration file may include several server
blocks distinguished by ports on which they listento
and by server names. Once nginx decides which server
processes a request, it tests the URI specified in the request’s header against the parameters of the location
directives
defined inside the server
block.
Add the following location
block to the server
block:
location / { root /data/www; }
This location
block specifies the “/
” prefix compared with the URI from the request. For matching requests, the URI will be added to the path specified in the root directive,
that is, to /data/www
, to form the path to the requested file on the local file system. If there are several matching location
blocks nginx selects the one with the longest prefix. The location
block above provides the
shortest prefix, of length one, and so only if all otherlocation
blocks fail to provide a match, this block will be used.
Next, add the second location
block:
location /images/ { root /data; }
It will be a match for requests starting with /images/
(location /
also matches such requests, but has shorter prefix).
The resulting configuration of the server
block should look like this:
server { location / { root /data/www; } location /images/ { root /data; } }
This is already a working configuration of a server that listens on the standard port 80 and is accessible on the local machine at http://localhost/
. In response to requests with URIs starting with /images/
, the server will send files
from the /data/images
directory. For example, in response to the http://localhost/images/example.png
request nginx will send the /data/images/example.png
file. If such file does not exist, nginx will send a response indicating
the 404 error. Requests with URIs not starting with /images/
will be mapped onto the /data/www
directory. For example, in response to the http://localhost/some/example.html
request nginx will send the/data/www/some/example.html
file.
To apply the new configuration, start nginx if it is not yet started or send the reload
signal to the nginx’s master process, by executing:
nginx -s reload
In case something does not work as expected, you may try to find out the reason inaccess.log
anderror.log
files in the directory/usr/local/nginx/logs
or/var/log/nginx
.
Setting Up a Simple Proxy Server
One of the frequent uses of nginx is setting it up as a proxy server, which means a server that receives requests, passes them to the proxied servers, retrieves responses from them, and sends them to the clients.
We will configure a basic proxy server, which serves requests of images with files from the local directory and sends all other requests to a proxied server. In this example, both servers will be defined on a single nginx instance.
First, define the proxied server by adding one more server
block to the nginx’s configuration file with the following contents:
server { listen 8080; root /data/up1; location / { } }
This will be a simple server that listens on the port 8080 (previously, the listen
directive has not been specified since the standard port 80 was used) and maps all requests to the /data/up1
directory on the local file system. Create
this directory and put the index.html
file into it. Note that the root
directive is placed in theserver
context. Such root
directive is used when the location
block selected for serving a request
does not include own root
directive.
Next, use the server configuration from the previous section and modify it to make it a proxy server configuration. In the first location
block, put the proxy_pass directive
with the protocol, name and port of the proxied server specified in the parameter (in our case, it is http://localhost:8080
):
server { location / { proxy_pass http://localhost:8080; } location /images/ { root /data; } }
We will modify the second location
block, which currently maps requests with the /images/
prefix to the files under the /data/images
directory, to make it match the requests of images with typical file extensions. The
modified location
block looks like this:
location ~ \.(gif|jpg|png)$ { root /data/images; }
The parameter is a regular expression matching all URIs ending with .gif
, .jpg
, or .png
. A regular expression should be preceded with ~
. The corresponding requests will be mapped to the /data/images
directory.
When nginx selects a location
block to serve a request it first checks location directives that specify prefixes, remembering location
with
the longest prefix, and then checks regular expressions. If there is a match with a regular expression, nginx picks this location
or, otherwise, it picks the one remembered earlier.
The resulting configuration of a proxy server will look like this:
server { location / { proxy_pass http://localhost:8080/; } location ~ \.(gif|jpg|png)$ { root /data/images; } }
This server will filter requests ending with .gif
, .jpg
, or .png
and map them to the /data/images
directory (by adding URI to the root
directive’s parameter) and pass all other requests to the
proxied server configured above.
To apply new configuration, send the reload
signal to nginx as described in the previous sections.
There are many more directives that may be used to further configure a proxy connection.
Setting Up FastCGI Proxying
nginx can be used to route requests to FastCGI servers which run applications built with various frameworks and programming languages such as PHP.
The most basic nginx configuration to work with a FastCGI server includes using the fastcgi_pass directive instead of the proxy_pass
directive,
and fastcgi_param directives to set parameters passed to a FastCGI server. Suppose the FastCGI server is accessible on localhost:9000
. Taking
the proxy configuration from the previous section as a basis, replace the proxy_pass
directive with the fastcgi_pass
directive and change the parameter to localhost:9000
. In PHP, the SCRIPT_FILENAME
parameter
is used for determining the script name, and the QUERY_STRING
parameter is used to pass request parameters. The resulting configuration would be:
server { location / { fastcgi_pass localhost:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; } location ~ \.(gif|jpg|png)$ { root /data/images; } }
This will set up a server that will route all requests except for requests for static images to the proxied server operating on localhost:9000
through the FastCGI protocol.
以上就介绍了Nginx: Beginner’s Guide,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

phpoffersvarioussessionsionsavehandlers:1)文件:默認,簡單的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的session是用於在服務器端保存用戶數據以在多個請求之間保持狀態的機制。具體來說,1)session通過session_start()函數啟動,並通過$_SESSION超級全局數組存儲和讀取數據;2)session數據默認存儲在服務器的臨時文件中,但可通過數據庫或內存存儲優化;3)使用session可以實現用戶登錄狀態跟踪和購物車管理等功能;4)需要注意session的安全傳輸和性能優化,以確保應用的安全性和效率。

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

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

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能