Nginx給同一個域名配置多個項目
2020-10-28
BeatlesLadyBirds
It編程技術(shù)交流
一、概述
使用Nginx要在同一個域名下配置多個項目有兩種方式:
a.?nginx按不同的目錄分發(fā)給不同的項目;
b.?啟用二級域名,不同的項目分配不同的二級域名。
二、nginx按不同的目錄分發(fā)給不同的項目
nginx.conf配置部分:
server?{
listen?80;
server_name?example.com;
location ^~ /project1/ {
? ? proxy_pass? ? http://localhost:8081/;
? ? proxy_set_header? Host? ? ? $host;
? ? proxy_set_header? X-Real-IP? ? $remote_addr;
? ? proxy_set_header? X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /project2/ {
? ? proxy_pass? ? http://localhost:8082/;
? ? proxy_set_header? Host? ? ? $host;
? ? proxy_set_header? X-Real-IP? ? $remote_addr;
? ? proxy_set_header? X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
? proxy_pass? ? http://localhost:8080;
? proxy_set_header? Host? ? ? $host;
? proxy_set_header? X-Real-IP? ? $remote_addr;
? proxy_set_header? X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
這里配置了三個項目:
http://example.com/project1路徑分發(fā)到http://localhost:8081/
http://example.com/project2路徑分發(fā)到http://localhost:8082/
其他路徑分發(fā)到http://localhost:8080
關(guān)鍵點:
location?^~?/project2/
project2前后都要加斜杠,否則域名映射有問題。
proxy_pass?http://localhost:8082/;
路徑最后加斜杠:映射的是http://localhost:8082;
路徑最后不加斜杠:映射的是http://localhost:8082/project2。
實際應(yīng)用中,根據(jù)需要選擇。
完整nginx.conf:
#user? nobody;
worker_processes? 1;
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid? ? ? ? logs/nginx.pid;
events {
? ? worker_connections? 1024;
}
http {
? ? include? ? ? mime.types;
? ? default_type? application/octet-stream;
? ? #log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
? ? #? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? #? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
? ? #access_log? logs/access.log? main;
? ? sendfile? ? ? ? on;
? ? #tcp_nopush? ? on;
? ? #keepalive_timeout? 0;
? ? keepalive_timeout? 65;
? ? #gzip? on;
? ? server {
? ? ? ? listen? ? ? 8089;
? ? ? ? server_name? localhost;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? location / {
? ? ? ? ? ? #root? html;
? ? ? ? ? ? #index? index.html index.htm;
? ? ? ? }
location ^~/jwt/ {
? ? ? ? ? proxy_pass http://172.20.8.186:8080/;
? proxy_set_header? Host? ? ? $host;
? ? ? #proxy_cookie_path? /jwt? / ;
? proxy_set_header? X-Real-IP? ? $remote_addr;
? ? ? proxy_set_header? X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? }
location ^~/home/ {
? ? ? ? ? proxy_pass http://localhost:8082/;
? ? ? ? ? proxy_set_header? Host? ? ? $host;
? ? ? proxy_set_header? X-Real-IP? ? $remote_addr;
? ? ? proxy_set_header? X-Forwarded-For $proxy_add_x_forwarded_for;
}
? ? ? ? #error_page? 404? ? ? ? ? ? ? /404.html;
? ? ? ? # redirect server error pages to the static page /50x.html
? ? ? ? #
? ? ? ? error_page? 500 502 503 504? /50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root? html;
? ? ? ? }
? ? ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? proxy_pass? http://127.0.0.1;
? ? ? ? #}
? ? ? ? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? root? ? ? ? ? html;
? ? ? ? #? ? fastcgi_pass? 127.0.0.1:9000;
? ? ? ? #? ? fastcgi_index? index.php;
? ? ? ? #? ? fastcgi_param? SCRIPT_FILENAME? /scripts$fastcgi_script_name;
? ? ? ? #? ? include? ? ? ? fastcgi_params;
? ? ? ? #}
? ? ? ? # deny access to .htaccess files, if Apache's document root
? ? ? ? # concurs with nginx's one
? ? ? ? #
? ? ? ? #location ~ /\.ht {
? ? ? ? #? ? deny? all;
? ? ? ? #}
? ? }
? ? # another virtual host using mix of IP-, name-, and port-based configuration
? ? #
? ? #server {
? ? #? ? listen? ? ? 8000;
? ? #? ? listen? ? ? somename:8080;
? ? #? ? server_name? somename? alias? another.alias;
? ? #? ? location / {
? ? #? ? ? ? root? html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
? ? # HTTPS server
? ? #
? ? #server {
? ? #? ? listen? ? ? 443 ssl;
? ? #? ? server_name? localhost;
? ? #? ? ssl_certificate? ? ? cert.pem;
? ? #? ? ssl_certificate_key? cert.key;
? ? #? ? ssl_session_cache? ? shared:SSL:1m;
? ? #? ? ssl_session_timeout? 5m;
? ? #? ? ssl_ciphers? HIGH:!aNULL:!MD5;
? ? #? ? ssl_prefer_server_ciphers? on;
? ? #? ? location / {
? ? #? ? ? ? root? html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
}
三、啟用二級域名,不同的項目分配不同的二級域名
server?{
listen?80;
server_name?example.com;
location?/?{
proxy_pass?http://localhost:8080;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
}
}
#project1
server?{
listen?80;
server_name?project1.example.com;
location?/?{
proxy_pass?http://localhost:8081;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
}
}
#project2
server?{
listen?80;
server_name?project2.example.com;
location?/?{
proxy_pass?http://localhost:8082;
proxy_set_header?Host?$host;
proxy_set_header?X-Real-IP?$remote_addr;
proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
}
}
說明:
域名example.com映射到http://localhost:8080;
域名project1.example.com映射到http://localhost:8081;
域名project2.example.com映射到http://localhost:8082;
注意:通過代理后,訪問域名就可以訪問到對應(yīng)的項目。
四、如何選擇
如果是單獨子應(yīng)用,有足夠的域名可用,推薦使用第二種方式。測試環(huán)境,推薦使用第一種方式。