CentOS 7下安装配置Nginx支持伪静态
Linux下安装和配置Nginx支持伪静态对新手来说是一个难题,经常会遇到404等错误,配置调试很是浪费时间,有时候往往觉得无从下手,网上的资料也通常将一些重点难点的东西,给人一种只见树木不见森林的感觉,无法窥其全貌,本博给一个CentOS 7下安装配置Nginx支持伪静态的实例,供大家参考。
博文内容包括两部分:
- 一键安装
- 配置Nginx
文章内容:
-
一键安装
使用lnmp一键安装包,命令如下:
wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5 && ./install.sh lnmp
详细参考: [https://lnmp.org/install.html]
- 配置Nginx
/usr/local/nginx/conf/nginx.conf配置内容如下,具体信息见代码注释
user www www; #用户
worker_processes auto;
error_log /home/wwwlogs/nginx_error.log crit; #错误日志输出
pid /usr/local/nginx/logs/nginx.pid; #对应进程
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
#fastcgi参数设置
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
#gzip支持
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
server_tokens off;
access_log off;
#站点参数配置
include vhost/default.conf;
}
/usr/local/nignx/conf/vhost/default.conf
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /home/www;
autoindex off;
#forbid spider to draw the resources
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js|sql|html|htm|log|txt|doc|pdf|bak)$ {
valid_referers none blocked localhost *.baidu.com *.haosou.com *.google.com *.google.hk *.sogou.com *.etao.com *.chinaz.com;
if ($invalid_referer) {
return 403;
break;
}
access_log off;
expires 30d;
}
#阻止下载各种资源文件
location ~* ^.+\.(jpg|jpeg|gif|png|rar|zip|css|js|sql|html|htm|log|txt|doc|bak)$ {
if (-f $request_filename) {
root /statics/;
return 403;
}
}
#将url改写成index.php模式,比如http://localhost/admin/logout -> http://localhost/index.php/admin/logout
location / {
index index.html index.htm index.php;
rewrite ^/$ /index.php last;
rewrite ^/(?!index\.php|robots\.txt|images|js|styles|statics|install)(.*)$ /index.php/$1 last;
}
location ~ ^/favicon\.ico$ {
root /home/www;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#fastcgi配置支持pathinfo和伪静态
include enable-php.conf;
}
/usr/local/nignx/conf/enable-php.conf
location ~ [^/]\.php(/|$)
{
#设置$fastcgi_script_name和$fastcgi_path_info
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi_params;
}
所有的参数统一在一个文件内配置如下:
/usr/local/nignx/conf/fastcgi_params
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
#尤其注意如下四个参数SCRIPT_FILENAME和PATH_INFO默认文件内可能没有,需要添加
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";
参考文章:
nginx官方文章
https://blog.jjonline.cn/linux/218.html
https://blog.csdn.net/m0_37355951/article/details/78333723