简体   繁体   中英

nginx location pointing to an ip instead of a folder

So I have a website running on laravel in http://example.com sitting on server-A having a 111.111.111.111 ip and I have a wordpress sitting on server-B having a 222.222.222.222

I want it so that I can access my wordpress through http://example.com/mywordpress

how can I achieve this? I tried googling but I cant seem to get the keyword right

for now this is the best thing I can come up with

# my nginx conf

location /mywordpress {
  proxy_pass http://mywordpress.example.com/; //already set the 222.222.222.222 to server-B
}

assuming that you have a /etc/nginx/sites-available/example.com on server-A

open and add this line :

location /mywordpress {
    proxy_pass http://222.222.222.222/mywordpress;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

after

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

on server-B assuming that you had your wordpress installed on your root /var/www/html and only wordpress files existed

follow this guide https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

create new directory /var/www/html/mywordpress

move all your files from /var/www/html to /var/www/html/mywordpress

copy index.php and .htaccess from mywordpress directory to your html folder

edit the /var/www/html/index.php

require( dirname( __FILE__ ) . '/wp-blog-header.php' );

to

require( dirname( __FILE__ ) . '/mywordpress/wp-blog-header.php' );

and now, try accesing your new example.com/mywordpress

Try something like this:

http {

    upstream wordpress {
         server 222.222.222.222:80 max_fails=2  fail_timeout=3s;
    }

    server {

        listen 80;

        server_name example.com;

        location /mywordpress {
             rewrite /mywordpress(.*) /$1  break;
             proxy_pass http://wordpress
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM