文章标签 ‘nginx’
2021九月17

Nginx 匹配路径让请求走不同代理

location /api/v1 {
        set $is_matched 0;
        if ($request_uri ~ /api/v1/user/100001/ ) {
            proxy_pass https://127.0.0.1:1001/;
            set $is_matched 1;
        }
        
        if ($request_uri ~ /api/v1/user/100002/ ) {
            proxy_pass https://127.0.0.1:1001/;
            set $is_matched 1;
        }

        # 没有匹配到,跳转到默认页面
        if ($is_matched = 0) {
            proxy_pass https://127.0.0.1:8080;
        }
        
    }
2016八月19

Nginx:绑定域名代理tomcat

server {
	listen 80;
	listen 443 ssl;
	if ( $scheme = "http" ) {
            rewrite ^(.*) https://$host$1;
	}
	server_name nutzwk.wizzer.cn;
	root "/java/work/nutzwk";	
	ssl_certificate /java/work/1_nutzwk.wizzer.cn_bundle.crt;
	ssl_certificate_key /java/work/2_nutzwk.wizzer.cn.key;
	add_header "X-UA-Compatible" "IE=Edge, chrome=1";
	location / {
		proxy_pass http://127.0.0.1:9091;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}
}
2016七月4

Nginx:配置静态资源304缓存

location ~ \.(jpg|png|jpeg|gif)$  {
        expires 30d;
        root   /data/www/wizzer.cn/;
}

location ~ \.(js|css)$  {
        expires 2h;
        root   /data/www/wizzer.cn/;
}
2015十二月20

Nginx+Tomcat负载均衡、绑定多域名、设置开机启动的安装步骤

1、JDK安装
/data1/soft/java/
>>chmod 777 jdk-6u45-linux-x64-rpm.bin
>>jdk-6u45-linux-x64-rpm.bin
>>gedit /etc/profile
JAVA_HOME=/data1/soft/java/jdk1.6.0_45

CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME
export CLASSPATH
export PATH
>>source /etc/profile

2、TOMCAT安装
将原windows系统中的tomcat主要文件拷贝到linux(conf、lib、bin),并新建空文件夹(logs、temp、work)
/data1/soft/tomcat1 多网站的那个tomcat
/data1/soft/tomcat2 少网站的那个tomcat
分别修改两个tomcat下的server.xml文件,将项目路径分别替换为 /data1/www/websties1 和 /data1/www/websties2
>>/data1/soft/tomcat/bin/startup.sh //启动tomcat
>>/data1/soft/tomcat/bin/shutdown.sh //停止tomcat

开机启动:
http://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html
【tomcat1、tomcat2分别设置为启动项】
除了百度经验之外,要在catalina.sh 里加上下面三行,否则在自启动的时候找不到jdk
export JAVA_HOME=/data1/soft/java/jdk1.6.0_45
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

3、Nginx安装
>>rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
>>yum -y install nginx
通过命令安装后
配置文件:/etc/nginx/nginx.conf
域名配置:/etc/nginx/conf.d/ 一个域名一个配置文件,不要的域名可以备份后删除

server {
listen 80;
server_name test.wizzer.cn;
location / {
proxy_pass http://test.wizzer.cn:8101;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

日志文件路径:/var/logs/nginx
服务启动或通知
>>service nginx stop
>>service nginx start
设置为随机启动
>>chkconfig nginx on

4、Nginx代理权限设置
若通过域名+端口号可访问,但80端口显示 502 Bad Gateway ,日志/var/logs/nginx/error.log报
2015/12/19 18:02:03 [crit] 9978#0: *8 connect() to 61.132.139.155:8101 failed (13: Permission denied) while connecting to upstream, client: 124.73.13.21, server: test.wizzer.cn, request: “GET / HTTP/1.1”, upstream: “http://61.132.139.155:8101/”, host: “test.wizzer.cn”
则需要设置selinux权限(宽容模式):
>>setenforce 0
>>setsebool -P httpd_can_network_connect 1

5、防火墙配置
>>vi /etc/sysconfig/iptables
#########################################################
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 21 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited
COMMIT
#########################################################

>>/etc/init.d/iptables restart #重启防火墙使配置生效,或者命令 service iptables restart

2015十二月15

LNMP:Nginx+PHP Ci框架配置注意事项

CodeIgniter

php.ini  修改:

cgi.fix_pathinfo=1

 

nginx/vhost/ 域名配置:

 

server {
listen 80;
server_name www.wizzer.cn wizzer.cn;
root “/data/www/www.wizzer.cn”;
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?$1 last;
break;
}
}
location ~ \.php(.*)$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}

2014五月28

Windows :Nginx + PHP 配置虚拟目录

首先配置好运行环境:

windows下配置nginx+php环境

其次修改nginx配置文件:

server {
        listen       80;
        server_name  localhost;
	location ~ ^/bbs/.+\.php$ {
	   alias		E:/xuetang/cn/bbs;
	   rewrite		/bbs/(.*\.php?) /$1 break;
	   fastcgi_index	index.php;
	   fastcgi_pass		127.0.0.1:9000;
	   fastcgi_param	SCRIPT_FILENAME E:/xuetang/cn/bbs$fastcgi_script_name;
	   include		fastcgi_params;
	}
	location ~ ^/bbs($|/.*) {
	   alias		E:/xuetang/cn/bbs/$1;
	   index		index.php index.html index.htm;
	}

        location / {
            root   E:/xuetang/cn/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           E:/xuetang/cn/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

注意虚拟目录配置要在根目录上面。。