詳解Nginx配置多站點(diǎn)需要踩的坑
從Windows下的Apache轉(zhuǎn)移到Linux下的Nginx,發(fā)現(xiàn)有很多坑需要踩。
以下就做個(gè)簡單的記錄,方便后來者爬坑。
配置Nginx,一般會(huì)遇到以下幾個(gè)坑:
- 配置nginx支持pathinfo模式
- - 優(yōu)化URL,隱藏index.php
- - 同一服務(wù)器配置多站點(diǎn)
配置pathinfo模式
在server(也就是你的站點(diǎn),一個(gè)server對(duì)應(yīng)一個(gè)站點(diǎn))中輸入以下內(nèi)容:
location ~ ^(.+\.php)(.*)$ {
root html/[站點(diǎn)目錄]; #配置站點(diǎn)目錄路徑
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
其中root html/[站點(diǎn)目錄]這個(gè)一定要填寫跟你server的站點(diǎn)目錄路徑,要不它默認(rèn)為html,從而導(dǎo)致路徑解析失敗。會(huì)出現(xiàn)的現(xiàn)象就是,在之前配置的location中定義了root路徑也無法生效,訪問http://localhost會(huì)跳轉(zhuǎn)到/usr/local/nginx/html/index.php[index.html],因?yàn)槲业膎ginx是源碼安裝,所以路徑可能不同,yum安裝的話一般會(huì)在/usr/share/nginx/html/index.php[index.html].(PS:原默認(rèn)關(guān)于fastcgi的配置可以注釋掉)
隱藏index.php
隱藏index.php,大多數(shù)是采用Nginx的重寫規(guī)則來進(jìn)行的。
下面,就是博主的列出的一個(gè)參考:
location / {
root html/[站點(diǎn)目錄];
index index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1;
}
}
這里的站點(diǎn)目錄是你程序(框架)的index.php所在的目錄。例如,CI框架的話,root html/ci,其中index.php位于html/ci/index.php.
本質(zhì)上,隱藏index.php文件就是重寫URL。具體詳細(xì)用法可以參考Nginx重寫模塊的官方文檔:Module ngx_http_rewrite_module
同一服務(wù)器多站點(diǎn)配置
一般一臺(tái)服務(wù)器不會(huì)單一的運(yùn)行一個(gè)站點(diǎn),往往是運(yùn)行多個(gè)站點(diǎn)的。
在Nginx配置多站點(diǎn)是非常簡單,便捷的。正如,前面所說的,一個(gè)server對(duì)應(yīng)一個(gè)站點(diǎn)。例如:
server {
listen 80;
server_name www.leslie.net.cn;
location / {
....
}
}
server {
listen 80;
server_name www.hellomyfrend.top;
location / {
....
}
}
這樣,就配置了兩個(gè)站點(diǎn),分別為www.leslie.net.cn和www.hellomyfrend.top.
這里貼一份配置文件作為參考:
user nginx nginx;
worker_processes 2;
#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;
rewrite_log on;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name pay.zf2.com;
location / {
root html/zf2/pay/public;
index index.php index.html index.htm;
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1;
}
}
location ~ ^(.+\.php)(.*)$ {
root html/zf2/pay/public;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
}
server {
listen 80;
server_name mp.zf2.com;
location / {
root html/zf2/server/public;
index index.php index.html index.htm;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/$1;
}
}
location ~ ^(.+\.php)(.*)$ {
root html/zf2/server/public;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
負(fù)載均衡下的webshell上傳+nginx解析漏洞的過程
這篇文章主要介紹了負(fù)載均衡下的webshell上傳+nginx解析漏洞,首先介紹了負(fù)載均衡下webshell上傳的四大難點(diǎn)及環(huán)境搭建教程,感興趣的朋友跟隨小編一起看看吧2024-02-02
Nginx實(shí)現(xiàn)外網(wǎng)訪問內(nèi)網(wǎng)的步驟詳解
外網(wǎng)瀏覽器與內(nèi)網(wǎng)是不通的,但是外網(wǎng)與中間過渡服務(wù)器是通的,中間過渡服務(wù)器與內(nèi)網(wǎng)服務(wù)器是通的,這樣在外網(wǎng)訪問過渡服務(wù)器時(shí),過渡服務(wù)器再跳轉(zhuǎn)到后臺(tái)服務(wù)器,本文給大家介紹了Nginx外網(wǎng)訪問內(nèi)網(wǎng)如何實(shí)現(xiàn)步驟,需要的朋友可以參考下2023-10-10
Nginx實(shí)現(xiàn)if多重判斷配置方法示例
這篇文章主要介紹了Nginx實(shí)現(xiàn)if多重判斷配置方法示例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
Nginx配置PATHINFO隱藏thinkphp index.php
這篇文章主要介紹了Nginx配置PATHINFO隱藏thinkphp index.php,本文直接給出配置示例,需要的朋友可以參考下2015-07-07
Nginx使用limit_req_zone對(duì)同一IP訪問進(jìn)行限流的方法
今天小編就為大家分享一篇Nginx使用limit_req_zone對(duì)同一IP訪問進(jìn)行限流的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
nginx 配置location匹配規(guī)則實(shí)例講解
在本篇文章里小編給大家整理的是關(guān)于nginx 配置location匹配規(guī)則實(shí)例講解內(nèi)容,需要的朋友們學(xué)習(xí)下。2020-03-03

