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

4
  • 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? Commented Jun 23, 2021 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. Commented Jun 23, 2021 at 16:19
  • Usually PHP-FPM handler block looks like location ~ \.php$ { ... fastcgi_pass <socket>; ... } or something similar. Commented Jun 23, 2021 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. Commented Jun 23, 2021 at 16:33

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much would you mind explaining it a little further please?
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 question and the very first part of my answer.
No hurries whenever you could, it would be great! Sure it works pretty good. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.