Ngnix rewrite PHP files loaded

yes, I know this question has already been asked and unfortunately, even though I've tried different answers, none of them seem to work on my case.

Basically, I'm trying to do a mod rewrite with Ngnix, but for some reason on some pages it will take you to the correct page, and on other pages it will just load the file.

This is my default site config file:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name studiowolfree.com;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #       proxy_pass http://127.0.0.1:8080;
    #}

    #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 /usr/share/nginx/html;
    #}

    # pass the PHP s to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            # With php5-cgi alone:
            # fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

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

    # nginx configuration

    error_page 500 /error.php?id=500;
    error_page 404 /error.php?id=404;
    error_page 403 /error.php?id=403;

    autoindex off;

    location /articles
    {
            rewrite ^(.*)$ /articles.php last;
            rewrite ^(.*)$ /articles.php last;
    }

    location /workshops
    {
            rewrite ^(.*)$ /workshops.php last;
            rewrite ^(.*)$ /workshops.php last;
    }

    location /account
    {
            rewrite ^(.*)$ /account.php last;
            rewrite ^(.*)$ /account.php last;
    }

    location /article
    {
            rewrite ^/article/([^/]*)$ /article.php?shortname=$1 last;
            rewrite ^/article/([^/]*)/$ /article.php?shortname=$1 last;
    }

    location /category
    {
            rewrite ^/category/([^/]*)$ /category.php?shortname=$1 last;
            rewrite ^/category/([^/]*)/$ /category.php?shortname=$1 last;
    }

    location /users
    {
            rewrite ^/users/([^/]*)$ /users.php?id=$1 last;
            rewrite ^/users/([^/]*)/$ /users.php?id=$1 last;
    }

    location /workshop
    {
            rewrite ^/workshop/([^/]*)$ /workshop.php?id=$1 last;
            rewrite ^/workshop/([^/]*)/$ /workshop.php?id=$1 last;
            rewrite ^/workshop/([^/]*)$ /workshop.php?id=$1 last;
            rewrite ^/workshop/([^/]*)/$ /workshop.php?id=$1 last;
            rewrite ^/workshop/([^/]*)/item/([^/]*)$ /workshop.php?id=$1&step=$2 last;
            rewrite ^/workshop/([^/]*)/$ /workshop.php?id=$1 last;
            rewrite ^/workshop/([^/]*)$ /workshop.php?id=$1 last;
            rewrite ^/workshop/([^/]*)/item/([^/]*)/$ /workshop.php?id=$1&step=$2 last;
    }

    location /.htaccess
    {
            deny all;
    }
}

      

The weird thing is that some urls go to the corresponding files (for example, for account successfully reaches /account.php) and some just download the source.

Yes, all files exist.

What could be causing this problem?

+3


source to share


1 answer


Make sure all files are owned by nginx first, and also change the user running from php-fpm to nginx. This will allow both of these to process files.

Also try putting this in your nginx config file.

location / {
    try_files $uri $uri/ /index.php?q=$request_uri;
    }

      



Instead

location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
}

      

0


source







All Articles