WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Permalinks Nginx not working - Help required (12 posts)

  1. medsha
    Member
    Posted 1 year ago #

    Hi there people.

    I recently switched over from Apache to Nginx and as most of you know... The rewriting structure is totally different and the commands in .htaccess become invalid.

    I have been browsing the internetz for a while and I have stumbled upon many topics in this section of this website and many other how to... And somehow, I do not manage to get it working on my domain.

    I have installed the "Nginx Compatibility" plugin and I have tried to do as instructed, but to no avail. I would prefer to do things without the plugin, but if it is necessary, then yea... :) I will not mind using it.

    This is the data I use, can anyone, please help me out? Oh, I did manage to get the rewrite on and everything, but there was 1 huge problem and that is: When it's switched on, I cannot access the admin section... So there is something wrong in the code I use...

    Please help me figuring out how to get it on. >:)

    This is the default file that has no rewrite commands - I would prefer to use this without plugins.

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. Not the blockquote. ]

    # You may add here your
    # server {
    #	...
    # }
    # statements for each of your virtual hosts
    
    server {
    
    	listen   80; ## listen for ipv4
    	listen   [::]:80 default ipv6only=on; ## listen for ipv6
    
    	server_name  the website.net;
    	server_name_in_redirect off;
    
    fastcgi_buffer_size             32k;
    fastcgi_buffers                 16 32k;
    fastcgi_busy_buffers_size       64k;
    fastcgi_temp_file_write_size    64k;
    
    	location / {
    		root   /var/www;
    		index  index.html index.htm index.php;
    	}
    
    	# redirect server error pages to the static page /50x.html
    	#
    	#error_page   500 502 503 504  /50x.html;
    	#location = /50x.html {
    	#	root   /var/www/nginx-default;
    	#}
    
    	# 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 		/var/www;
    		include 	/etc/nginx/fastcgi_params;
    		fastcgi_pass  	127.0.0.1:9000;
    		fastcgi_index 	index.php;
    		try_files $uri @wordpress;
    
    		fastcgi_param  	SCRIPT_FILENAME /var/www/$fastcgi_script_name;
    	}
    
    	location /phpmyadmin {
                   root /usr/share/;
                   index index.php index.html index.htm;
                   location ~ ^/phpmyadmin/(.+\.php)$ {
                           try_files $uri =404;
                           root /usr/share/;
                           fastcgi_pass 127.0.0.1:9000;
                           fastcgi_index index.php;
                           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                           include /etc/nginx/fastcgi_params;
                   }
                   location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                           root /usr/share/;
                   }
            }
            location /phpMyAdmin {
                   rewrite ^/* /phpmyadmin last;
            }
    
    	# deny access to .htaccess files, if Apache's document root
    	# concurs with nginx's one
    	#
    	#location ~ /\.ht {
    	#	deny  all;
    	#}
    }

    What I also need to say is, I really hate the default part that is added in the permalinks section which is: index.php - This led to 404 errors, and is the main reason why I downloaded nginx compatibilty plugin. If there is a simple code that can be added to the provided file to remove the index.php part from the link, I will be really thankful to you. ;)

    I hope that I can receive the help here. :)

    These are the suggestions by the plugin creator which I don't manage to implement in my file: http://wordpress.org/extend/plugins/nginx-compatibility/other_notes/

    I run Nginx. 0.7.67

  2. Converting my main site to nginx has been on my todo list for ages...

    When it's switched on, I cannot access the admin section... So there is something wrong in the code I use...

    I'm running nginx 1.1.9 (nginx -v) so this might not work for you. Make a backup copy of your configuration file first and see if you can add this above your location ~ \.php$ section.

    location / {
                    try_files $uri $uri/ /index.php;
            }

    Really, make sure you have that backup of that file first.

  3. rahul286
    Member
    Posted 1 year ago #

    @ medsha
    You are using a lot unwanted code.

    Try following config:

    server {
            server_name example.com www.example.com;
    
            access_log   /var/log/nginx/example.com.access.log;
            error_log    /var/log/nginx/example.com.error.log;
    
            root /var/www/example.com/htdocs;
            index index.php;
    
            location / {
                    try_files $uri $uri/ /index.php?$args;
            }
    
            location ~ \.php$ {
                    include fastcgi_params;
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
            }
    }

    For more details, refer to http://rtcamp.com/wordpress-nginx/tutorials/

    Also you can use http://wordpress.org/extend/plugins/nginx-helper/ for removing index.php from permalinks

  4. medsha
    Member
    Posted 1 year ago #

    @Jan Dembowski

    Nope, that didn't do the trick.

    When I add the suggested command line above the location ~ \.php$ I get the error

    Restarting nginx: [emerg]: duplicate location "/" in /etc/nginx/sites-enabled/default:41
    configuration file /etc/nginx/nginx.conf test failed

    After that happened, I tried to implement

    try_files $uri $uri/ /index.php; into location ~ \.php$ itself
    I did disable the try_files $uri @wordpress; part in the code when I tried your code. :)

    It resulted into 404 errors when I turn on custom permalinks.

    Any other suggestions maybe?

  5. Pothi
    Member
    Posted 1 year ago #

    @medsha

    Please remove the entire location / block, in your current configuration ...

    location / {
    		root   /var/www;
    		index  index.html index.htm index.php;
    	}

    *and then*, please insert what's suggested by Jan, for location / block...

    location / {
        try_files $uri $uri/ /index.php;
    }

    I hope this helps.

  6. medsha
    Member
    Posted 1 year ago #

    @Pothi Kalimuthu

    By following your suggestion only the homepage becomes reachable. If you try to access the admin panel or any random post/page, you will be redirected to the homepage again.

  7. Pothi
    Member
    Posted 1 year ago #

    Can you please check the error logs and see, if you can figure out what's happening?

  8. Note: Previous solicitation post deleted.

    These forums are for volunteer support only and offering to do paid work here will get your account moderated.

  9. medsha
    Member
    Posted 1 year ago #

    @Rahul286

    I tried your code and it doesn't work either.

    @Pothi

    I am trying to find anything special in the logs, but I can't find anything. Do you have any suggestions what to look for? Maybe I just didn't pay enough attention in finding the culprit in the error logs.
    ----------------------------------------

    I do have to tell you that the data is not in my nginx.conf - it's in the default file.

    I'll post the content again of what I exactly have.

    nginx.conf

    user www-data;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log;
    pid        /var/run/nginx.pid;
    
    events {
        worker_connections  1024;
        # multi_accept on;
    }
    
    http {
        include       /etc/nginx/mime.types;
    
        access_log	/var/log/nginx/access.log;
        error_log /var/log/nginx/error.log info;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
        tcp_nodelay        on;
    
        gzip  on;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }

    File default located in /etc/nginx/sites-enabled/

    # You may add here your
    # server {
    #	...
    # }
    # statements for each of your virtual hosts
    
    server {
    
    	listen   80; ## listen for ipv4
    	listen   [::]:80 default ipv6only=on; ## listen for ipv6
    
    	server_name  3rabgamer.net;
    	server_name_in_redirect off;
    
    fastcgi_buffer_size             32k;
    fastcgi_buffers                 16 32k;
    fastcgi_busy_buffers_size       64k;
    fastcgi_temp_file_write_size    64k;
    
    	location / {
    		root   /var/www;
    		index  index.html index.htm index.php;
    	}
    
    	# redirect server error pages to the static page /50x.html
    	#
    	#error_page   500 502 503 504  /50x.html;
    	#location = /50x.html {
    	#	root   /var/www/nginx-default;
    	#}
    
    	# 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 		/var/www;
    		include 	/etc/nginx/fastcgi_params;
    		fastcgi_pass  	127.0.0.1:9000;
    		fastcgi_index 	index.php;
    		try_files $uri @wordpress;
    
    		fastcgi_param  	SCRIPT_FILENAME /var/www/$fastcgi_script_name;
    	}
    
    	location /phpmyadmin {
                   root /usr/share/;
                   index index.php index.html index.htm;
                   location ~ ^/phpmyadmin/(.+\.php)$ {
                           try_files $uri =404;
                           root /usr/share/;
                           fastcgi_pass 127.0.0.1:9000;
                           fastcgi_index index.php;
                           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                           include /etc/nginx/fastcgi_params;
                   }
                   location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                           root /usr/share/;
                   }
            }
            location /phpMyAdmin {
                   rewrite ^/* /phpmyadmin last;
            }
    
    	# deny access to .htaccess files, if Apache's document root
    	# concurs with nginx's one
    	#
    	#location ~ /\.ht {
    	#	deny  all;
    	#}
    }

    Maybe that can clear something up now. Thanks for your time, guys.

  10. rahul286
    Member
    Posted 1 year ago #

    Replace ur current /etc/nginx/sites-enabled/default file with following content:

    server {
            server_name 3rabgamer.net;
    
            root /var/www;
            index index.php;
    
            location / {
                    try_files $uri $uri/ /index.php?$args;
            }
    
            location ~ \.php$ {
    		include 	/etc/nginx/fastcgi_params;
    		fastcgi_pass  	127.0.0.1:9000;
    		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            }
    }

    It might work except phpmyadmin part (I will help you on that once this part gets sorted)

  11. medsha
    Member
    Posted 1 year ago #

    Hey Rahul,

    This indeed finally solved the problem. I took care of phpmyadmin and the problem is finally solved!

    This is the current code I use that allows my website to use proper permalinks:

    server {
            server_name 3rabgamer.net;
    
            root /var/www;
            index index.php;
    
            location / {
                    try_files $uri $uri/ /index.php?$args;
            }
    
            location ~ \.php$ {
    		include 	/etc/nginx/fastcgi_params;
    		fastcgi_pass  	127.0.0.1:9000;
    		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            }
    
    	location /phpmyadmin {
                   root /usr/share/;
                   index index.php index.html index.htm;
                   location ~ ^/phpmyadmin/(.+\.php)$ {
                           try_files $uri =404;
                           root /usr/share/;
                           fastcgi_pass 127.0.0.1:9000;
                           fastcgi_index index.php;
                           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                           include /etc/nginx/fastcgi_params;
                   }
                   location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                           root /usr/share/;
                   }
            }
            location /phpMyAdmin {
                   rewrite ^/* /phpmyadmin last;
            }
    
    }

    This is the code in the /sites-available/default file. Thanks for your help guys.

  12. rahul286
    Member
    Posted 1 year ago #

    @medsha

    Glad to know it helped! :-)

Topic Closed

This topic has been closed to new replies.

About this Topic