Flarum使用nginx做重定向配置

文章详细描述了如何在Nginx配置文件中设置SSL监听、SSL缓存和跨域访问控制,同时提到如何在Apache中修改默认端口以避免与Nginx冲突,并提供了重启服务的命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进入nginx.conf

# vim /etc/nginx/nginx.conf

 配置为下面内容


events {
    # 配置项,例如 worker_processes 和 worker_connections
}

http {
    # 其他 http 配置项...

    server {
        listen 80;
        server_name faq.example.com;

        return 301 https://faq.example.com$request_uri;
    }

    server {
        listen 443 ssl;
        server_name faq.example.com;

        ssl on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        ssl_certificate /etc/nginx/ssl/faq.pem;    # 证书路径
        ssl_certificate_key /etc/nginx/ssl/faq.key; # 请求认证 key 的路径

        location / {
            # 允许跨域请求的“域”
            add_header 'Access-Control-Allow-Origin' $http_origin;
            # 允许客户端提交 Cookie
            add_header 'Access-Control-Allow-Credentials' 'true';
            # 允许客户端的请求方法
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
            # 允许客户端提交的请求头
            add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization';
            # 允许客户端访问的响应头
            add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma';
            # 处理预检请求
            if ($request_method = 'OPTIONS') {
                # 预检请求缓存时间
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            proxy_pass http://192.168.100.100:8080;  # 注意这里修改为 8080 端口
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

            proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }
    }
}


Apache需要修改端口号,不然会和nginx的端口号冲突,ubuntu 的apache端口号在conf中修改并不生效,需要修改/etc/apache2/ports.conf

#vim /etc/apache2/ports.conf
Listen 8080

<IfModule ssl_module>
        Listen 8443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 8443
</IfModule>

重启apache

# systemctl restart apache2

启动nginx

# systemctl start nginx

user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { server { listen 80; server_name 192.168.3.250; root /var/www/flarum/public; index index.php; location / { try_files $uri $uri/ /index.php?$query_string; } location ~* \.php$ { fastcgi_pass unix:/run/php/php8.1-fpm.sock; include snippets/fastcgi-php.conf; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires max; add_header Cache-Control "public"; } ## # Basic Settings ## sendfile on; tcp_nopush on; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #} 检查以上代码
06-23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值