Nginx+Tomcat建站:自動https,配置全站favicon.ico,Linux下Nginx常用命令

Nginx版本:1.16.0

Tomcat版本:8.5.41

系統環境:CentOS Linux release 7.6.1810

Nginx在配置多Tomcat多域名時,需要代理多個Tomcat端口,我將所有域名的配置文件都放在Nginx的運行目錄下(/usr/local/nginx/conf/host),host爲新建的文件夾,每一個域名有一份配置文件。

將Nginx的配置文件中的server部分全部刪除並且在http的大括號裏面加入下面一段:

include /usr/local/nginx/conf/host/*.conf;

表示引入外部配置文件。

  • 配置https

準備材料:

域名

域名證書

關於服務器和域名以及證書這塊可以查看我的另外一篇文章:https://blog.csdn.net/u012667477/article/details/82793183

配置內容:

 server {
        listen       443 ssl;
        server_name  mp.idwarf.cn;


        ssl_certificate      ssl/1_mp.idwarf.cn_bundle.crt;
        ssl_certificate_key  ssl/2_mp.idwarf.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $http_host;
			proxy_set_header X-Forwarded-Proto https;
			proxy_redirect off;
			proxy_pass http://mp;
        }
	
    }
  • http自動跳轉https

配置內容:

        listen       80;
        server_name  mp.idwarf.cn;
        return       301 https://mp.idwarf.cn$request_uri;
  • 配置全站favicon.ico

準備材料:

ico格式的圖標,並將其命名爲favicon.ico

配置內容:

location ~ ^/favicon\.ico$ {
            root    /usr/local/nginx/conf/favicon;
        }

其中/usr/local/nginx/conf/favicon爲這個圖標所在的文件夾目錄

  • 完整配置文件

upstream mp{
	server 127.0.0.1:8081;
}
server {
        listen       80;
        server_name  mp.idwarf.cn;
        return       301 https://mp.idwarf.cn$request_uri;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass  http://mp;
			proxy_set_header Host $http_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   /usr/local/nginx/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 / {
        #    root   /usr/local/nginx/html;
        #    index  index.html index.htm;
        #}

        #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   /usr/local/nginx/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  mp.idwarf.cn;


        ssl_certificate      ssl/1_mp.idwarf.cn_bundle.crt;
        ssl_certificate_key  ssl/2_mp.idwarf.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $http_host;
			proxy_set_header X-Forwarded-Proto https;
			proxy_redirect off;
			proxy_pass http://mp;
        }
	
	location ~ ^/favicon\.ico$ {
            root    /usr/local/nginx/conf/favicon;
        }
    }
  • Nginx 常用命令

  1. Nginx下載地址:http://nginx.org/en/download.html
  2. Nginx啓動:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  3. 查看Nginx端口:ps -ef | grep nginx
  4. 停止Nginx:kill -9 上一個命令查詢到的端口號
  5. 重啓Nginx:/usr/local/nginx/sbin/nginx -s reload
  6. 檢查Nginx配置文件:/usr/local/nginx/sbin/nginx -t
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章