Yêu cầu thg 7 9, 2019 11:07 SA 944 1 4
  • 944 1 4
+1

Hỏi cách config nginx cho nhiều project trên cùng domain

Chia sẻ
  • 944 1 4

Như tiều đề thì mình đang thử config Nginx để chạy được nhiều project trên cùng 1 domain (khác path). domain.com/project1 thì sẽ gọi vào project 1, domain.com/project2 thì sẽ gọi vào project 2 (ở đây mình đang dùng Laravel). Mình thử config nhưng hiện tại đang bị lỗi 404 nhưng chưa tìm được hướng giải quyết.

Đây là file config của mình

server {
	listen   80;
	listen   [::]:80;

	# Make site accessible from http://localhost/
	server_name dev.demo.api;

	# Add stdout logging
	# error_log /var/log/nginx/demo/api.error.log;
	# access_log /var/log/nginx/demo/api.access.log;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to index.html
		try_files $uri $uri/ /index.php?$args;
	}

	location /setting {
		root /var/www/demo/setting.ws/public;
		index index.php index.html index.htm;
		try_files $uri $uri/ /index.php?$args;
	}

	error_page 404 /404.html;
        location = /404.html {
                root /var/www/errors;
                internal;
        }

	# pass the PHP scripts to FastCGI server listening on socket
	#
	location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                fastcgi_index index.php;
                include fastcgi_params;
	}
}

4 CÂU TRẢ LỜI


Đã trả lời thg 7 9, 2019 5:16 CH
Đã được chấp nhận
+4

Bạn có thể config theo kiểu reverse proxy như sau. Giả sử bạn có 2 project với đường dẫn như sau:

  • project1 - /var/www/demo/project1/public
  • project2 - /var/www/demo/project2/public
    Thì bạn có thể có file config như sau đối với project PHP (ở đây hình như bạn cũng dùng Laravel):

# config cho project 1
server {
        listen 9001 default_server;

        root /var/www/demo/project1/public;
        index index.php index.html index.htm;

        try_files $uri $uri/ @rewrite;

        location @rewrite {
        rewrite ^/(.*)$ /index.php;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        include fastcgi_params;
        fastcgi_pass unix:///var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param REALPATHTEST $realpath_root;
        internal;
    }
}

# config cho project 2
server {
        listen 9002 default_server;

        root /var/www/demo/project2/public;
        index index.php index.html index.htm;

        try_files $uri $uri/ @rewrite;

        location @rewrite {
        rewrite ^/(.*)$ /index.php;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }
        include fastcgi_params;
        fastcgi_pass unix:///var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param REALPATHTEST $realpath_root;
        internal;
    }
}

# config reversy proxy
server {
    listen   80;
    listen   [::]:80;

    server_name dev.demo.api;

    access_log /var/log/nginx/dev.access.log;
    error_log /var/log/nginx/dev.error.log;

    location ~ /project1/(.*)$ {
            proxy_pass      http://localhost:9001/$1?$query_string;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
    }

   location ~ /project2/(.*)$ {
            proxy_pass      http://localhost:9002/$1?$query_string;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
    }
}

Bạn có thể thử như cách trên và chỉ cần đổi lại phần đường dẫn của project 1 và 2 theo folder của bạn rồi restart lại nginx xem. Với cấu hình trên thì mọi request sẽ đi vào domain của bạn và tùy vào đường dẫn nó sẽ tự map được vào project tưởng ứng của bạn và truyền được hết thông tin về query string sang. Bạn có thể tham khảo thêm về set-up reverse proxy tại đây https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

Chia sẻ
Avatar Nguyen Tuan Anh @TuanAnh9996
thg 7 10, 2019 1:40 SA

thank bạn 😄

Đã trả lời thg 7 9, 2019 2:03 CH
+1

Làm được nhưng mà không nên bạn à, vì Laravel không recommend cách này, có lỗi gì chưa phát hiện thì bạn phải tự chịu 😄

Ref: https://serversforhackers.com/c/nginx-php-in-subdirectory

Chia sẻ
Avatar Nguyen Tuan Anh @TuanAnh9996
thg 7 10, 2019 1:38 SA

@pht à mình nghịch trên local thôi nên cứ yên tâm 😄

Đã trả lời thg 7 9, 2019 4:21 CH
+1

Bạn nên dùng sub-domain thay vì dùng path để phân biệt site con. Vì khi bạn phân biệt bằng path thì sẽ bị conflig với route của laravel rất mất thời gian, dễ nhầm lẫn và lỗi. Dùng sub-domain config dễ, nhanh và clear hơn rất nhiều.

Chia sẻ
Avatar Nguyen Tuan Anh @TuanAnh9996
thg 7 10, 2019 1:39 SA

subdomain thì mình config được rồi nhưng trên local nên nghịch chia path thôi 😄

Đã trả lời thg 7 10, 2019 6:00 SA
0

https://viblo.asia/p/cai-dat-va-cau-hinh-subdomain-tren-nginx-centos-7-vyDZOxjklwj

Trong bài viết này của mình có nói về việc cấu hình subdomain, mong bạn có thể xem

Chia sẻ
Avatar Mai Trung Đức @maitrungduc1410
thg 7 11, 2019 1:59 SA
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí