编程知识 cdmana.com

Nginx伪静态规则

https://bbs.aliyun.com/read/259101.html?spm=5176.7189909.5.41.tKLbLc
根据本帖我搭建了一个wordpress站点,除了主页所有的页面都是的404。
在网上找了下认为是“Nginx伪静态规则”,然后在“nginx.conf” 文件中没有server,只有events{...} 和http{...}。
请问此时我该如何处理?

user  www www;
worker_processes  8;


error_log  /alidata/log/nginx/error.log crit;
pid        /alidata/server/nginx/logs/nginx.pid;


#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;


events
{
  use epoll;
  worker_connections 65535;
}




http {
        include       mime.types;
        default_type  application/octet-stream;


        #charset  gb2312;


        server_names_hash_bucket_size 128;







采纳答案1:
ReNginx伪静态规则
/alidata/server/下并无httpd。
以下路径有一个phpwind.conf   不知是否为站点配置文件。
/alidata/server/nginx-1.4.4/conf/vhosts

-------------------------

回 3楼dongshan8的帖子
你好,我找到了server{} 。但伪静态还是没有成功。
server {
        listen       80;
        server_name  localhost;
        index index.html index.htm index.php;
        root /alidata/www/;
        location ~ .*\.(php|php5)?$
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }

        #α¾²Ì¬¹æÔò
        include /alidata/server/nginx/conf/rewrite/phpwind.conf;
        access_log  /alidata/log/nginx/access/http.log;
}

-------------------------

ReNginx伪静态规则
我是完全按照这个网站来进行配置的。
help.aliyun.com/knowledge_detail/5973947.html?pos=1
伪静态规则的路径是cd /alidata/server/nginx/conf/rewrite
配置文件的路径是/alidata/server/nginx-1.4.4/conf/vhosts

server {
        listen       80;
        server_name  localhost;
        index index.html index.htm index.php;
        root /alidata/www/;
        location ~ .*\.(php|php5)?$
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
        #α¾²Ì¬¹æÔò
        include /alidata/server/nginx/conf/rewrite/wordpress.conf       ;
        access_log  /alidata/log/nginx/access/http.log;
}

-------------------------

ReNginx伪静态规则
rewrite]# vi wordpress.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;
        }
}

这次应该是对了,可是403了

-------------------------

回 5楼dongshan8的帖子
我是完全按照这个网站来进行配置的。
help.aliyun.com/knowledge_detail/5973947.html?pos=1
伪静态规则的路径是cd /alidata/server/nginx/conf/rewrite/wordpress.conf
vi wordpress.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;
        }
}


配置文件的路径是/alidata/server/nginx-1.4.4/conf/vhosts/phpwind.conf
vi phpwind.conf
server {
        listen       80;
        server_name  localhost;
        index index.html index.htm index.php;
        root /alidata/www/;
        location ~ .*\.(php|php5)?$
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
        #α¾²Ì¬¹æÔò
        include /alidata/server/nginx/conf/rewrite/wordpress.conf       ;
        access_log  /alidata/log/nginx/access/http.log;
}
以上设置完成后,由404变成403……
然后更改了一下nginx.conf的设置,把user的www www改为root不知道对不对,请老师指点。
/alidata/server/nginx/conf
vi nginx.conf。
user  root;
worker_processes  8;

error_log  /alidata/log/nginx/error.log crit;
pid        /alidata/server/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}


http {
        include       mime.types;
        default_type  application/octet-stream;

        #charset  gb2312;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 8m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

        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 128k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        #limit_zone  crawler  $binary_remote_addr  10m;
        log_format '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
        include /alidata/server/nginx/conf/vhosts/*.conf;
}

-------------------------

回 10楼dongshan8的帖子
你好,在网上查了一下可能是的default.conf需要更改,不知是不是这两个的问题

可否贴一下cd /alidata/server/nginx/conf下nginx.conf.default
还有cd /alidata/server/nginx/conf/vhosts/下default.conf.bak

-------------------------

回 12楼dongshan8的帖子
你好,这个设置是没问题的,依旧主页以外403。
会不会是index文件缺失的问题,请问这个文件应该在www目录下的什么位置

-------------------------

回 14楼dongshan8的帖子
^^^^^^^^^^^^^^^^

-------------------------

回 16楼dongshan8的帖子
已解决!非常感谢!



采纳答案2:
楼主您好,

欢迎来到阿里云论坛。

请问,您是使用“Linux一键Web环境”安装包安装的 nginx + php 环境的吗?

如果是,那按照安装包里的使用说明,您的站点配置文件路径应该是在:/alidata/server/httpd/conf/vhosts 目录里喔。

站点的配置文件里应该有server{}的配置内容哩。

-------------------------

回 2楼(大古) 的帖子
您好,

不好意思,如果您当前使用的是nginx,那路径应该不是httpd的。

那您看到的 phpwind.conf 文件中的内容是不是有 server{} 配置内容呢?

-------------------------

回 4楼(大古) 的帖子
您好,

那具体到您的wordpress站点静态化,您是将静态化规则内容放到哪个配置文件呢?

可以贴出包含有静态化规的配置文件内容吗?

-------------------------

回 8楼(大古) 的帖子
您好,

然后更改了一下nginx.conf的设置,把user的www www改为root不知道对不对

一般情况下,用 root 身份运行web程序是很危险的,所以不建议您修改 nginx 默认的运行用户。

我为您测试一下,按照上边提到的教程是否能顺利完成wordpress的静态化。

-------------------------

楼主您好,

我尝试按照您提供的教程来操作一遍,wordpress的nginx静态化结果,好象正常喔。

-------------------------

Re:回 11楼(大古) 的帖子
好哩,

#user  nobody;
worker_processes  1;

#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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


server {
        listen       80;
        server_name  localhost;
    index index.html index.htm index.php;
    root /alidata/www/default;
    location ~ .*\.(php|php5)?$
    {
        #fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires 30d;
    }
    location ~ .*\.(js|css)?$
    {
        expires 1h;
    }
    #伪静态规则
    include /alidata/server/nginx/conf/rewrite/default.conf;
    access_log  /alidata/log/nginx/access/default.log;
}

-------------------------

回 13楼(大古) 的帖子
您好,

请问您的站点访问网址是什么呢?

-------------------------

回 15楼(大古) 的帖子
您好,

看到您是将wordpress的程序安装在子目录,并不是网站的根目录,所以“一键安装包”里自带的wordpress.conf的rewrite规则并不适合您的应用场景喔。

您可以新建一个适合安装在子目录里的wordpress静态化规则,如下边的内容:

location /wordpress/ {
    index index.php;
    try_files $uri $uri/ /wordpress/index.php?$args;
}

测试有效,效果如下图:



可参考: http://stackoverflow.com/questions/17805576/nginx-rewrite-in-subfolder




版权声明
本文为[阿里云问答]所创,转载请带上原文链接,感谢
https://developer.aliyun.com/ask/243124

Scroll to Top