0

I'm pretty new to nginx. I have two types of code one is simple php running through, index2.php and I have one directory named wordpress inside it has a whole wordpress website.

What I'm trying to achieve is to get them both running on the main domain without slashes with subdirectory names.

location ~ (/lottery-results|patternTwo) {
    try_files $uri /index2.php$is_args$args;
}
location / {
    try_files $uri /wordpress/index.php?$args;
}

This is the config I am currently using it works fine for my purpose. The first directive loads some urls through simple php in index2.php. The second directive loads wordpress, however when I open this url:

http://domain.test/

It send me to:

http://domain.test/wordpress/wp-admin/setup-config.php

My problem is that /wordpress part I want the url to be:

http://domain.test/wp-admin/setup-config.php

instead.

I tried to use alias and root but it didn't change anything, (honestly I don't get what alias and root even does!)

edit : PHP-FPM handler:

        location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_phpMAMP_PhpLocalhost_MAMP.sock;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          /Applications/MAMP/conf/nginx/fastcgi_params;
        }

Steve Moretz
  • 2,758
  • 1
  • 17
  • 31
  • You are trying to do something really strange. Why can't you place both `index2.php` and WordPress files into the same directory? What are those `lottery-results|patternTwo` strings? Why can't you use your first site under some URI prefx, e.g. `lottery-results`? Well, I think there is a way to make it work, you'll need to alter `REQUEST_URI` PHP-FPM parameter. Can you add your nginx PHP-FPM handler (`location` block) to your question? – Ivan Shatsky Jun 23 '21 at 15:37
  • Hi thanks for the comment, it's not just index2.php, index2.php runs a lot of other php files in it just like WordPress does through its index.php, those strings are other paths that index2.php supports, I can't put my first website under uri prefix because it has an api and the applications running with that api will all fail.I don't know what you mean by php-fpm Handler location block. – Steve Moretz Jun 23 '21 at 16:19
  • Usually PHP-FPM handler block looks like `location ~ \.php$ { ... fastcgi_pass ; ... }` or something similar. – Ivan Shatsky Jun 23 '21 at 16:22
  • @IvanShatsky I guess I found it I'm not sure though I'm using mamp pro and I found this in the main nginx.conf because the individual websites don't create different conf in mamp you can just use a ui editor. – Steve Moretz Jun 23 '21 at 16:33

1 Answers1

1

Lets try this:

# This block should be OUTSIDE the server block!
map $request_uri $new_uri {
    ~^/wordpress(/.*)  $1;
    default            $request_uri;
}

server {
    ...
    location ~ (/lottery-results|patternTwo) {
        try_files $uri /index2.php$is_args$args;
    }
    location / {
        try_files $uri /wordpress$uri /wordpress/index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;

        # Here the order of directives DOES matter
        # This should be the first:
        include /Applications/MAMP/conf/nginx/fastcgi_params;
        # This should be the second:
        fastcgi_param REQUEST_URI $new_uri;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_phpMAMP_PhpLocalhost_MAMP.sock;
    }
}
Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
  • Thank you so much would you mind explaining it a little further please? – Steve Moretz Jun 23 '21 at 20:10
  • Sure, but a little later, when I'll have a couple of free time. Does this configuration works as expected? Meanwhile you can check [this](https://stackoverflow.com/questions/62968482/expose-multiple-api-uri-on-the-same-nginx-server-block) question and the very first part of my answer. – Ivan Shatsky Jun 23 '21 at 20:17
  • No hurries whenever you could, it would be great! Sure it works pretty good. Thanks again. – Steve Moretz Jun 23 '21 at 20:32