nginx部署方式,两种写法,本人认为第一种写法没有第二种写法优雅
第一种写法配置文件:
server { listen 80; server_name youban-dev.jqtest.mopon.cn; root E:youban_svn2.1.0youban-phpmall; index index.php index.html index.htm; access_log logs/youban-dev.jqtest.mopon.cn.access.log; #PC端 location /pc/ { alias E:youban_svn2.1.0youban-phpmallwebsiteweb; rewrite ^(/pc/)/$ $1 permanent; try_files $uri /website/web/index.php?$args; } location ~ ^/pc/(.+.(js|json|html|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ { access_log off; expires 360d; rewrite ^/pc/(.+)$ /website/web/$1 break; rewrite ^/pc/(.+)/(.+)$ /website/web/$1/$2 break; try_files $uri =404; } #wap手机端 location /wap/ { alias E:youban_svn2.1.0youban-phpmallmobileweb; rewrite ^(/wap/)/$ $1 permanent; try_files $uri /mobile/web/index.php?$args; } location ~ ^/wap/(.+.(js|json|html|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ { access_log off; expires 360d; rewrite ^/wap/(.+)$ /mobile/web/$1 break; rewrite ^/wap/(.+)/(.+)$ /mobile/web/$1/$2 break; try_files $uri =404; } #微信端 location /weixin/ { alias E:youban_svn2.1.0youban-phpmallfrontendweb; rewrite ^(/weixin/)/$ $1 permanent; try_files $uri /frontend/web/index.php?$args; } location ~ ^/weixin/(.+.(js|json|html|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ { access_log off; expires 360d; rewrite ^/weixin/(.+)$ /frontend/web/$1 break; rewrite ^/weixin/(.+)/(.+)$ /frontend/web/$1/$2 break; try_files $uri =404; } #地图 location /map/ { alias E:youban_svn2.1.0youban-phpmallmapsweb; rewrite ^(/weixin/)/$ $1 permanent; try_files $uri /maps/web/index.php?$args; } location ~ ^/map/(.+.(js|json|html|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ { access_log off; expires 360d; rewrite ^/map/(.+)$ /maps/web/$1 break; rewrite ^/map/(.+)/(.+)$ /maps/web/$1/$2 break; try_files $uri =404; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { root E:youban_svn2.1.0youban-phpmall; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; } }
第二种写法配置
server { listen 80; server_name youban-dev.jqtest.mopon.cn; #微信站点 location /weixin/ { proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://youban-weixin/; } #wap站点 location /wap/ { proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://youban-wap/; } #pc站点 location /pc/ { proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://youban-pc/; } #地图站点 location /map/ { proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://youban-map/; } }
#微信站点
upstream youban-weixin { server 127.0.0.1:8012; } server { listen 8012; root E:youban_svn2.1.0youban-phpmallfrontendweb; index index.php index.html index.htm; location /nginx_status { stub_status on; access_log off; } location ~ .php$ { root E:youban_svn2.1.0youban-phpmallfrontendweb; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*.(js|css)?$ { expires 12h; } location ~ /. { deny all; } #access_log /home/logs/youban_dev/2.0/weixin.youban-dev.com.log; }
#wap站点
upstream youban-wap { server 127.0.0.1:8013; } server { listen 8013; root E:youban_svn2.1.0youban-phpmallmobileweb; index index.php index.html index.htm; location /nginx_status { stub_status on; access_log off; } location ~ .php$ { root E:youban_svn2.1.0youban-phpmallmobileweb; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*.(js|css)?$ { expires 12h; } location ~ /. { deny all; } #access_log /home/logs/youban_dev/2.0/h5.youban-dev.com.log; }
#pc站点
upstream youban-pc { server 127.0.0.1:8011; } server { listen 8011; root E:youban_svn2.1.0youban-phpmallwebsiteweb; index index.php index.html index.htm; location /nginx_status { stub_status on; access_log off; } location ~ .php$ { root E:youban_svn2.1.0youban-phpmallwebsiteweb; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*.(js|css)?$ { expires 12h; } location ~ /. { deny all; } #access_log /home/logs/youban_dev/2.0/web.youban-dev.com.log; }
#地图站点
upstream youban-map { server 127.0.0.1:8014; } server { listen 8014; root E:youban_svn2.1.0youban-phpmallmapsweb; index index.php index.html index.htm; location /nginx_status { stub_status on; access_log off; } location ~ .php$ { root E:youban_svn2.1.0youban-phpmallmapsweb; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*.(js|css)?$ { expires 12h; } location ~ /. { deny all; } #access_log /home/logs/youban_dev/2.0/web.youban-dev.com.log; }
OK,将配置文件引入nginx.conf配置文件,就能通过目录访问不同的应用了,如:
微信站点
http://www.xxx.com/weixin/
手机wap站点
http://www.xxx.com/wap/
PC站点
http://www.xxx.com/pc/
地图应用
http://www.xxx.com/map/
warning:无论哪种配置,过后都会影响到Url::to()这个方法,直接重写UrlManager中的createUrl的方法,如下:
<?php /** * User: szliugx@gmail.com * Date: 2017/4/12 * Time: 14:06 */ namespace mapscomponent; class UrlManager extends yiiwebUrlManager { /** * 重写URL,以自定义的栏目开始 * @param array|string $params * @return string */ public function createUrl($params) { $url = parent::createUrl($params); // TODO: Change the autogenerated stub return Yii::$app->params["homeUrl"]["map"].ltrim($url, '\/');; } }
配置文件中修改: