0. 2019/07更新
在开始学习nginx服务器时选择手动安装、配置,比较繁琐。笔者现在为了运维方便使用宝塔面板(www.bt.cn),目前为止没啥明显问题,主要看中面板上可以直接备份、加载数据库。
# 在conf文件中添加该内容,以支持URL中省略index.php
location / {
try_files $uri $uri/ /index.php?$args;
}
## 2020 近期转移主机,百度搜索如下的方法 并不能合理转移,奇怪:
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
conf文件中存放着Nginx的主要配置,建议域名与目录绑定,防止通过ip目录访问。
多虚拟主机配置:
1. /etc/nginx/nginx.conf
完整内容
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
# s1 直接ip访问此处根目录,
server {
listen 80 default_server;
listen [::]:80 default_server;
# 这里改动了,也可以写你的域名
server_name default_server;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
# 这里改动了 定义首页索引文件的名称
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# 这里新加的
# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
# Fastcgi服务器和程序(PHP,Python)沟通的协议.
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;# 设置监听端口
# 设置nginx的默认首页文件(上面已经设置过了,可以删除)
fastcgi_index index.php;
# 设置脚本文件请求的路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 引入fastcgi的配置文件
include fastcgi_params;
}
}
#S2 for demo.com
server {
listen 80 ;
listen [::]:80 ;
server_name www.demo.com;
root /usr/share/nginx/html/demo;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
index index.php index.html index.htm;# 这里改动了 定义首页索引文件的名称
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# 这里新加的
# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
# Fastcgi服务器和程序(PHP,Python)沟通的协议.
location ~ \.php$ {
# 设置监听端口
fastcgi_pass 127.0.0.1:9000;
# 设置nginx的默认首页文件(上面已经设置过了,可以删除)
fastcgi_index index.php;
# 设置脚本文件请求的路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 引入fastcgi的配置文件
include fastcgi_params;
}
}
#S3 conf for my blog
server {
listen 80;
listen [::]:80 ;
server_name blog.cn;
root /usr/share/nginx/html/blog;
index index.php index.html index.htm;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
# Fastcgi服务器和程序(PHP,Python)沟通的协议.
location ~ \.php$ {
# 设置监听端口
fastcgi_pass 127.0.0.1:9000;
# 设置nginx的默认首页文件(上面已经设置过了,可以删除)
fastcgi_index index.php;
# 设置脚本文件请求的路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 引入fastcgi的配置文件
include fastcgi_params;
}
}
}
2. 单独conf文件里配置虚拟主机,
没有做验证
<VirtualHost *:80>
ServerName anda.ccdot.cn
#ServerAlias
DocumentRoot /yjdata/www/wwwroot/jswebsite/
DirectoryIndex index.html index.php
</VirtualHost>
<Directory "/yjdata/www/wwwroot/jswebsite/">
Options +Includes -Indexes
AllowOverride All
Order Deny,Allow
Allow from All
</Directory>
#
<VirtualHost *:80>
ServerName example2.com
ServerAlias example2.com www.example2.com
DocumentRoot /var/www/test2/
DirectoryIndex index.html index.php
</VirtualHost>
<Directory "/var/www/test2/“>
Options +Includes -Indexes
AllowOverride All
Order Deny,Allow
Allow from All
</Directory>