Nginx服务器伪静态配置文件 nginx.conf和rewrite.conf 详解

作者:     目录: 服务器     发表: 2020年03月15日

Nginx里使用伪静态是直接在nginx.conf中写规则的,nginx.conf 文件在服务器的地址,我用的腾讯的云主机,系统是CentOS,登录后直接使用find -name ‘nginx.cong’ 命令查找nginx.conf文件即可,我的nginx.conf 在 /etc/nginx/nginx.conf,具体见下图:

Nginx服务器伪静态配置文件 nginx.conf和rewrite.conf 详解

可以使用coreFtp通过上面的地址找到nginx.conf,具体内容如下:

Nginx服务器伪静态配置文件 nginx.conf和rewrite.conf 详解

Nginx服务器中伪静态规则是在nginx.conf文件中规定的,里面有一个include语句,include /usr/share/nginx/html/rewrite.conf,也就是说nginx文件中把rewrite.conf文件包含进来了。

CentOS Nginx 操作语句

  1. 卸载nginx命令:yum remove nginx
  2. Nginx测试命令:nginx -t

Nginx服务器伪静态配置文件 nginx.conf和rewrite.conf 详解

腾讯云服务器手动搭建 LNMP 环境(CentOS 7)教程 :https://cloud.tencent.com/document/product/213/38056

Nginx 跳转到带www的域名,查了一下午资料,总算弄好了,原来是字符编码有问题。操,前面加了一段sever代码,listen字符出问题了,重启Nginx之前,先使用nginx -t 命令,检查一下 nginx.conf是否有问题,我的就是字符是乱码,改了一下,立马好了了,从网上复制代码的时候一定要注意,经常会碰到乱码问题。

Nginx服务器伪静态配置文件 nginx.conf和rewrite.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;

server {
listen 80;
server_name cad2d3d.com;
rewrite ^/(.*) http://www.cad2d3d.com/$1 permanent;
}

server {
 listen       80;
 root   /usr/share/nginx/html;
 server_name  www.cad2d3d.com;
 #charset koi8-r;
 #access_log  /var/log/nginx/log/host.access.log  main;
 #
 location / {
       index index.php index.html index.htm;
	   include /usr/share/nginx/html/rewrite.conf;
	   client_max_body_size 100m;
 }
  error_page 404 /404.html;
            location = /40x.html {
        }
 #redirect server error pages to the static page /50x.html
 #
 error_page   500 502 503 504  /50x.html;
 location = /50x.html {
   root   /usr/share/nginx/html;
 }
 #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 location ~ .php$ {
   fastcgi_pass   127.0.0.1:9000;
   fastcgi_index  index.php;
   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
   include        fastcgi_params;
 }
}			
    include /etc/nginx/conf.d/*.conf;
}

Post Comment

Your email address will not be published. Required fields are marked *