nginx做域名转发和uri转发

时间:2024-03-07 21:20:48

一、 域名转发 www.qq.com ------> www.baidu.com  

  nginx部署在192.168.1.100,本地配置host 192.168.1.100 www.qq.com ,浏览器输入www.qq.com会跳转到www.baidu.com

 

# vi  /etc/nginx/nginx.conf

user nginx;
worker_processes 1;

events {
use epoll;
worker_connections 10240;
}

http {

server {
listen 80;
server_name www.qq.com;
rewrite ^/(.*)$ http://www.baidu.com/$1 last;
}
}

 

 

二、 同一个域名内URI转发  www.qq.com/xxx ------> www.qq.com/main/xxx

  根目录设置为/data/gg,将所有对根目录的请求都转发到根目录下的main目录,请求带过来的参数不变

#vi /etc/nginx/nginx.conf
user nginx;
worker_processes 1;

events {
use epoll;
worker_connections 10240;
}

http {

server {
listen 80;
server_name localhost;
location / {
root /data/gg;
}
rewrite ^/([a-z0-9]+)$ /main/$1 last;
}
}

 

 

附nginx操作命令:

nginx version: nginx/1.5.7
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx//)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file

 

三、 nginx转发代理配置

location ^~ /idata/ {
proxy_pass http://www.idata.com/;
proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
}