# Re-writing URLs with Nginx...



## Brian07002 (Sep 15, 2011)

I have an issue on my hands with re-writing url's with nginx. 

I am running a LEMP server on Ubuntu 18.04 with flynax classifieds running as my cms (flynax.com)


I am able to open the http://localhost/classifieds/ url w/out any problems, but any links after that are giving me a 404 even though they do exist. If you can suggest anything I can try to resolve this, I appreciate it a lot. 


Here's my default (/etc/nginx/sites-available/) config file: 


```
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    index index.php index.htm index.html;
    root /var/www/html/;
    server_name localhost;
    



location classifieds {
    index index.php index.htm index.html;
    try_files $uri $uri/ index.php?$args;
    root /var/www/html/classifieds;
}










#---- flynax rewrite section start ---- #
#non last slash redirect
rewrite ^([^\.]*[^/])$ $1/ permanent;

#personal address subdomains rewrite
if ($host ~ ((?!www\.|m\.|mobile\.).*)\.(.*) ) {
set $subdomain $1;
}

if ( $subdomain ) {
rewrite ^(.*)$ index.php?page=$subdomain&wildcard&rlVareables=$request_uri last;
}

if (!-e $request_filename)
{
#define paging
rewrite ^/([^/]*)/((.*)/)?(index([0-9]*).ht(m?ml?))?$ index.php?page=$1&rlVareables=$3&pg=$5 last;

#define listing details
rewrite ^/([^/]*)/((.*)/)?(.+)-l?([0-9]+).ht(m|ml)$ index.php?page=$1&rlVareables=$3&listing_id=$5 last;

#define browse
rewrite ^/([^/]*)/(.*)(\.html|\.htm|\/)$ index.php?page=$1&rlVareables=$2 last;

#define single pages
rewrite ^/(.+)(\.html|\.htm|\/)$ index.php?page=$1 last;

#sitemap rewrite rules
rewrite ^/([a-z_]*)?sitemap([0-9]*).xml$ /plugins/sitemap/sitemap.php?search=google&number=$2&mod=$1 last;
}
#---- flynax rewrite section end ---- #









    
# pass PHP scripts to FastCGI server

    location ~ \.php$ {
        
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

    # With php-fpm (or other unix sockets):
    # With php-cgi (or other tcp sockets):
    # fastcgi_pass 127.0.0.1:9000;
              }









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









# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#    listen 80;
#    listen [::]:80;
#
#    server_name example.com;
#
#    root /var/www/example.com;
#    index index.html;
#
#    location / {
#        try_files $uri $uri/ =404;
#    }
#}

}
```


----------



## Brian07002 (Sep 15, 2011)

Anyone?


----------



## scriptbox (Dec 7, 2018)

You may need to configure the firewall. For example, if a UFW firewall is active on the Ubuntu web server, it may need to be further adjusted to support NGINX.

See this tutorial to enable the traffic that you want via UFW:

https://hostadvice.com/how-to/how-to-install-the-lemp-nginx-mariadb-and-php-stack-on-ubuntu-18-04-lts/

You need both OpenSSH & Nginx to have deployed UFW profiles on your Ubuntu systems to enable both HTTP/HTTPS traffic.


----------



## Brian07002 (Sep 15, 2011)

scriptbox said:


> You may need to configure the firewall. For example, if a UFW firewall is active on the Ubuntu web server, it may need to be further adjusted to support NGINX.
> 
> See this tutorial to enable the traffic that you want via UFW:
> 
> ...


Hello,

Just to be clear, what I mean is that all my links are on localhost, but no matter what they cannot be found 404 error. If I turn off disable mod_rewrite all the the urls become good again, not 404 error so I believe this has nothing to do with ufw, but instead is a mis-configuration in the nginx config file some where.

Thank you though for your answer
Brian


----------



## scriptbox (Dec 7, 2018)

Can you go through the steps here to check your NGINX installation? There is a sample copy of the nginx.conf file for reference:

+ https://hostadvice.com/how-to/how-to-tune-and-optimize-performance-of-nginx-web-server/

Also review the configuration settings for worker processes, connections, & multi-accept values.


----------

