nginx配置只能域名访问,禁止ip访问

直接上配置

nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user              root;
worker_processes  1;

error_log  /opt/nginx/logs/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /opt/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

   gzip         on;
   gzip_min_length  1000;
   gzip_buffers  4 8k;
   gzip_http_version  1.1;
   gzip_types  text/text text/plain text/xml text/css application/x-javascript application/javascript;
   gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 #仅可通过域名访问,(具体的域名配置在default.conf的server中)不能通过ip访问
 server {
    listen 80 default;
	server_name _;
    return 404; 
} 
    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;

}

/etc/nginx/conf.d/default.conf的配置

upstream fsmap-upstream {
#反向代理到你真实的tomcat的地址和端口
    server 192.168.6.135:8080;
}

server {
    listen 80;
	#配置可以访问的域名
	server_name zrzwjj.com www.zrzwjj.com;
    location / {
        proxy_pass http://fsmap-upstream;
    }

    location ~ .*\.(css|js) {
        proxy_pass http://fsmap-upstream;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_cache cache_one;
        proxy_cache_valid 200 302 1h;
        proxy_cache_valid 301 6h;
        proxy_cache_valid any 12h;
        expires 1d;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /opt/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /opt/nginx/html;
    }

}

配置完之后需要配置被指向的tomcat的地址,配置tomcat的server.xml文件,具体配置如下

<!-- 配置具体的域名访问 -->    
 <Host name="www.zrzwjj.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
      <Context path="" docBase="/opt/war/fs-map.war" />
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>

<!-- 当时用ip访问的时候,设置跳转到报错的网站,其网站部署路劲指向errorapps -->   	 
	  <Host name="192.168.135.157"  appBase="errorapps"  
            unpackWARs="true" autoDeploy="true"  
            xmlValidation="false" xmlNamespaceAware="false"/>  

 

转载于:https://my.oschina.net/zrz1992/blog/1531858

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值