環境:
OS:CentOS 7.3 (1611)
Nginx:1.10.2
PHP:5.6.30
PHP-FPM:5.6.30
設定:
/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
/etc/nginx/conf.d/web.conf
server {
listen 80;
server_name 192.168.1.2;
location / {
index index.html index.php index.htm;
root /var/www/html;
}
location ~* /Web {
alias //share/Web/;
index index.html;
location ~ .*/(imgs|javascripts|styles)/ {
include /etc/nginx/mime.types;
}
location ~* .*\.(php|html)$ {
if ($fastcgi_script_name ~* /Web/(.*\.php)$) {
set $valid_fastcgi_script_name $1;
}
if ($fastcgi_script_name ~* /Web/(.*\.html)$) {
set $valid_fastcgi_script_name $1;
}
fastcgi_pass unix:/var/run/php-fpm/php56-fpm.sock;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME /share/Web/$valid_fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php56-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
需求:
想要讓使用者無論輸入 http://192.168.1.2/Web 或是 http://192.168.1.2/Web/ 時,都會自動連到 http://192.168.1.2/Web/index.html
問題:
但依目前這個設定值,最後網址都會變成 http://192.168.1.2/Web/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/index.html/ 這種網址,造成網頁找不到,還請各位大神協助指點迷津,謝謝。
location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
expires 14d;
}
6.同一台主機用socket來處理fastcgi是對的. 盡量不要用IP.
7.您使用alias, 表示路徑已取代了$document_root, 因此需要解開為:
fastcgi_param SCRIPT_FILENAME $document_root$1;
這樣Nginx才能將php檔透過fastcgi交給php-fpm解譯.
如果您是初學, 建議您不要用虛擬路徑(如/Web).
以下的設定只是對應我的說明, 我並沒有時間驗證下面的設定, 也需要實際的環境,僅提供參考:
server {
listen 80;
server_name 192.168.1.2;
index index.html index.php index.htm;
location / {
root /var/www/html;
try_files $uri $uri/ =404;
}
...
location ~* /Web/ {
alias /share/Web/;
try_files $uri $uri/ =404;
}
...
location ~* ^/Web/(.+\.php)$ {
alias /share/Web/;
fastcgi_pass unix:/var/run/php-fpm/php56-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$1;
include fastcgi_params;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php56-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
...
}
...
}
Google可以告訴您很多設定範例. 建議您從簡單的開始.
Nginx官方有基礎的設定:
https://www.nginx.com/resources/wiki/start/topics/examples/fastcgiexample/
https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/
也有教學:
https://www.linode.com/docs/web-servers/nginx/install-and-configure-nginx-and-php-fastcgi-on-ubuntu-16-04
Joomla的Nginx例子:https://docs.joomla.org/nginx